2012-08-12 14 views
8

Chciałem stworzyć coś takiego suwaka: enter image description hereAndroid Wielu State

Czy istnieje sposób na Androida zdefiniować trzy suwak państwa? Jestem nowy w rozwoju Androida, więc proszę podać jak najwięcej przykładowego kodu, byłoby to bardzo pomocne.

+0

znasz stanie rozwiązać tego jeszcze? Jeśli tak, proszę zamieścić swoją odpowiedź! – Milan

+0

Myślę, że wszystko, czego potrzebujesz, to niestandardowy pasek ocen: http://www.b2creativedesigns.com/ratingbar.php –

Odpowiedz

5

Prostym przykładem może być poniższy kod. Możesz grać trochę grafiką, a dostaniesz to, czego chcesz.

układ/main.xml

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/TableLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" > 

    <include 
     android:id="@+id/tableRow1_ref" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/description" /> 

    <SeekBar 
     android:id="@+id/seekBar1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="10dp" 
     android:max="100" /> 

    <include 
     android:id="@+id/tableRow1_ref" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     layout="@layout/description" /> 

    <SeekBar 
     android:id="@+id/seekBar2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:max="100" /> 

</TableLayout> 

układ/description.xml

<?xml version="1.0" encoding="utf-8"?> 
<TableRow xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/tableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="left" 
     android:paddingLeft="5dp" 
     android:text="@string/kid" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:text="@string/adult" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:gravity="right" 
     android:paddingRight="5dp" 
     android:text="@string/senior" /> 

</TableRow> 

MainActivity.java

package com.test.Slider; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 
import android.widget.SeekBar; 
import android.widget.SeekBar.OnSeekBarChangeListener; 

public class MainActivity extends ActionBarActivity 
     implements OnSeekBarChangeListener{ 

    SeekBar sb1, sb2; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     sb1 = (SeekBar)findViewById(R.id.seekBar1); 
     sb2 = (SeekBar)findViewById(R.id.seekBar2); 

     sb1.setOnSeekBarChangeListener(this); 
     sb2.setOnSeekBarChangeListener(this); 
    } 

    @Override 
    public void onProgressChanged(SeekBar seekBar, int progress, 
      boolean fromUser) { 
     // we don't need it 
    } 

    @Override 
    public void onStartTrackingTouch(SeekBar seekBar) { 
     // we don't need it  
    } 

    @Override 
    public void onStopTrackingTouch(SeekBar seekBar) { 
     int mProgress = seekBar.getProgress(); 
     if(mProgress > 0 & mProgress < 26) { 
      seekBar.setProgress(0); 
     } else if(mProgress > 25 & mProgress < 76) { 
      seekBar.setProgress(50); 
     } else seekBar.setProgress(100); 
    } 
} 

wyjściowa:

screenshot of sliders

1

Istnieje duża biblioteka to: android-comboseekbar. Użyj go lub zobacz, jak to działa.

Ekran Screen

+0

Podczas gdy ten link może odpowiedzieć na pytanie, lepiej umieścić tutaj istotne części odpowiedzi i podać link do odniesienie. Odpowiedzi dotyczące linków mogą stać się nieprawidłowe, jeśli strona z linkami się zmieni. - [Z recenzji] (/ opinia/niskiej jakości-posts/17377976) –