Android应用程序中的离散搜索栏?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2614714/
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 06:33:56  来源:igfitidea点击:

Discrete seekbar in Android app?

androidseekbar

提问by vee

I would like to create a seekbar for an Android app that allows the user to select a value between -5 and 5 (which maps to "strongly disagree" and "strongly agree"). How do I make a seekbar with discrete values? or is there a better UI widget I could use for this?

我想为 Android 应用程序创建一个搜索栏,允许用户选择 -5 和 5 之间的值(映射到“非常不同意”和“非常同意”)。如何制作具有离散值的搜索栏?或者是否有更好的 UI 小部件可以用于此目的?

Thanks.

谢谢。

回答by Jay Askren

The Seekbar works great for discrete values. We use a Seekbar for discrete data as shown below. To show which item is selected, we just change the font of the selected text view so it is bigger. You could also highlight by changing the background color or something. It works pretty well. You will want to call setMax(11) on the seek bar, and then in your code you need to translate between the range (0 through 11) and (-5 through 5) appropriately.

Seekbar 非常适用于离散值。我们对离散数据使用 Seekbar,如下所示。要显示选择了哪个项目,我们只需更改所选文本视图的字体,使其变大。您还可以通过更改背景颜色或其他内容来突出显示。它工作得很好。您将需要在搜索栏上调用 setMax(11),然后在您的代码中,您需要在范围(0 到 11)和(-5 到 5)之间进行适当的转换。

 <LinearLayout 
           android:orientation="vertical"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:gravity="center_horizontal">
     <LinearLayout 
           android:orientation="horizontal"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:gravity="center_horizontal">
    <TextView android:text="-5"
            android:id="@+id/answerNegative5"
            android:textSize="25sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView android:text="-4"
            android:id="@+id/answerNegative4"
            android:textSize="25sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    <TextView android:text="-3"
            android:id="@+id/answerNegative3"
            android:textSize="25sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    .....
  </LinearLayout>

  <SeekBar android:id="@+id/intensitySlider"
                    android:layout_weight="0"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />
</LinearLayout>

回答by Prashant Jajal

Check this linkIf you want to implement discrete seekbar with number of gaps without using third party library then use style property of seekbar.

检查此链接如果您想在不使用第三方库的情况下实现具有间隙数量的离散搜索栏,请使用搜索栏的样式属性。

<SeekBar
     android:id="@+id/sb"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:max="10"
     android:thumb="@drawable/ic_location"
     android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />

enter image description here

在此处输入图片说明

回答by Pentium10

I don't know what is the issue here, you add a seekbar having a range of 0-10. Then you can map these values to -5 if you substract -5 from the selected value.

我不知道这里有什么问题,您添加了一个范围为 0-10 的搜索栏。然后,如果从所选值中减去 -5,则可以将这些值映射到 -5。

EDIT

编辑

add android:max="10"to the xml definiton and you get a fixed size seekbar.

添加android:max="10"到 xml 定义中,您将获得一个固定大小的搜索栏。

You maybe should consider to add a textview to denote the current selected value's textual representation such as: Strongly Disagree. To update this view, subscribe to onProgressChangedevent and progressparameter will give you the chosen number.

您可能应该考虑添加一个 textview 来表示当前选定值的文本表示,例如:强烈不同意。要更新此视图,订阅onProgressChanged事件和progress参数将为您提供选择的数字。

SeekBar s = (SeekBar) findViewById(R.id.SeekBar);
s.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

                    @Override
                    public void onProgressChanged(SeekBar seekBar,
                            int progress, boolean fromUser) {
                                    }
}

回答by Balaji Gunasekar

I hope this code surely helpes you. Try this...

我希望这段代码肯定对你有帮助。尝试这个...

 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));

         }
     });
 }

回答by Mickey Tin

1-st step : set maxVal to 10;

第一步:将 maxVal 设置为 10;

2-nd step :

第二步:

@Override
public void onProgressChanged(SeekBar seekBar, int progress,
        boolean fromUser) 
   {
    switch(seekBar.getId())
    {
    case R.id.mySeekBar:
        int prValue = progress - 5;
        editText.setText(String.valueOf(preValue));
        break;
    }
}

回答by Pratibha Sarode

      <SeekBar
                android:id="@+id/seekbar"
                style="@style/SeekBarWithoutSteps"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="22dp"
                android:layout_marginRight="22dp"
                android:layout_marginTop="@dimen/margin_10"
                android:max="4"
                android:maxHeight="@dimen/margin_5"
                android:minHeight="@dimen/margin_5"
                android:paddingLeft="@dimen/margin_10"
                android:paddingRight="@dimen/margin_10"
                android:progressBackgroundTint="@color/colorGray"
                android:progressTint="@color/colorGreen"
                android:theme="@style/Widget.AppCompat.SeekBar.Discrete"
                android:thumb="@drawable/filled_green"
                android:thumbOffset="15dp" />