如何使用默认值在android中设置搜索栏..?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26399539/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:20:40  来源:igfitidea点击:

how to set seek bar in android with default values..?

android

提问by krishnan

The maximum value of SeekBaris 100 then How to set a Seekbarvalues default as 50 (starting at 50%). The seek bar will be start at 50.

的最大值SeekBar为 100,然后如何将Seekbar值设置为默认值 50(从 50% 开始)。搜索栏将从 50 开始。

I got a sample code but it will start at 0 to 100. Please answer me if you have any idea... Thank you.

我有一个示例代码,但它会从 0 到 100 开始。如果您有任何想法,请回答我...谢谢。

     public class MainActivity extends Activity implements OnSeekBarChangeListener{
     private SeekBar bar; // declare seekbar object variable
     // declare text label objects
     private TextView textProgress,textAction;
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
    super.onCreate(savedInstanceState);
    // load the layout
    setContentView(R.layout.activity_main);     
    bar = (SeekBar)findViewById(R.id.seekBar1); // make seekbar object
    bar.setOnSeekBarChangeListener(this); // set seekbar listener.
    // since we are using this class as the listener the class is "this"

    // make text label for progress value
    textProgress = (TextView)findViewById(R.id.textViewProgress);
    // make text label for action
    textAction = (TextView)findViewById(R.id.textViewAction);

    }

    @Override
    public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) {
    // TODO Auto-generated method stub

    // change progress text label with current seekbar value
    textProgress.setText("The value is: "+progress);
    // change action text label to changing
    textAction.setText("changing");
    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    textAction.setText("starting to track touch");

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    // TODO Auto-generated method stub
    seekBar.setSecondaryProgress(seekBar.getProgress());
    textAction.setText("ended tracking touch");     
    }

     }

回答by Muhammed Refaat

in XML :

在 XML 中:

android:progress="50" // or any other value 

in Java :

在 Java 中:

mSeekbar.setProgress(50); // or any other value 

and if you don't know what's the Max Value and just want to make it 50%:

如果您不知道 Max Value 是多少而只想将其设为50%

mSeekbar.setProgress(mSeekbar.getMax()/2);

回答by Syed Raza Mehdi

See thisand here is how you can do

看到这个,这是你可以做的

 seekBar.setMax(0);
 seekBar.setMax(100);
 seekBar.setProgress(50);

in xml you can do like this android:progress="50"

在 xml 你可以这样做 android:progress="50"

回答by shellym

Set the MaxValue once before you set the progress value . In that way the system knows what proportion of the seekbar has to be colored.

在设置进度值之前设置一次 MaxValue。通过这种方式,系统知道必须对搜索栏的多大比例进行着色。