如何在搜索栏上显示某些值作为默认值?

时间:2012-01-10 17:21:26

标签: android default seekbar

我有一个搜索栏,例如:从0到10.如果我想将搜索栏设置为5作为默认位置,我该如何制作。所以在这个例子中,seekbar应该从中间开始。

5 个答案:

答案 0 :(得分:37)

使用此

android:progress="5"

与“progress”类似,它从ProgressBar继承了它的一些属性。您可以在http://developer.android.com/reference/android/widget/SeekBar.html

找到所有属性

答案 1 :(得分:20)

要使Seekbar值为5

在xml布局中,将最大值用作10

  android:max="10"

在java for seekbar中取得进展5

 seekbar = (SeekBar) findViewById(R.id.seekBar1);
 seekbar.setProgress(5);

就是这样:))

答案 2 :(得分:4)

我希望这段代码能够对你有所帮助 试试这个......

float discrete=0; 
 float start=0; 
 float end=100; 
 float start_pos=0; 
 int start_position=0; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    start=-10;      //you need to give starting value of SeekBar 
end=10;         //you need to give end value of SeekBar 
    start_pos=5;    //you need to give starting position value of SeekBar 

    start_position=(int) (((start_pos-start)/(end-start))*100); 
    discrete=start_pos; 
    SeekBar seek=(SeekBar) findViewById(R.id.seekBar1); 
    seek.setProgress(start_position); 
    seek.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { 

        @Override 
        public void onStopTrackingTouch(SeekBar seekBar) { 
            // TODO Auto-generated method stub 
            Toast.makeText(getBaseContext(), "discrete = "+String.valueOf(discrete), Toast.LENGTH_SHORT).show(); 
        } 

        @Override 
        public void onStartTrackingTouch(SeekBar seekBar) { 
            // TODO Auto-generated method stub 

        } 

        @Override 
        public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { 
            // TODO Auto-generated method stub 
            // To convert it as discrete value 
            float temp=progress; 
            float dis=end-start; 
            discrete=(start+((temp/100)*dis)); 

        } 
    }); 
} 

答案 3 :(得分:1)

默认值由 xml :

android:progress="5"

xml 的最大值

android:max="10"

答案 4 :(得分:-1)

final Timer timer = new Timer();

seekBar_efficiency.setMax(40);

seekBar_efficiency.setProgress(35);
timer.schedule(new TimerTask() {

    @Override
    public void run() {
        seekBar_efficiency.setProgress(20);
        timer.cancel();
    }
}, 200, 50);