Android Number Picker 获取 textview 的价值

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

Android Number Picker get value to textview

androidnumberpicker

提问by user3618526

I am using an alert dialog with a number picker, but I don't know a easy way to get te number picker value to an textview in the activity when I press the OK button of the alert.

我正在使用带有数字选择器的警报对话框,但是当我按下警报的“确定”按钮时,我不知道一种将数字选择器值获取到活动中文本视图的简单方法。

Activity java code:

活动java代码:

public class SecondActivity extends Activity {

public void button2 (View v){

    View v1 = getLayoutInflater().inflate(R.layout.dialog1, null);

    NumberPicker picker = (NumberPicker) v1.findViewById(R.id.np1);
    picker.setMinValue(1);
    picker.setMaxValue(20);
    picker.setWrapSelectorWheel(false);
    //final int number = picker.getValue();

    AlertDialog.Builder builder =  new AlertDialog.Builder(this);
    builder.setView (v1);
    builder.setTitle("Cantidad");
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            return;
        } });
    builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            return;
        } }); 

    AlertDialog dialog = builder.create();
    dialog.show();
}

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

}

}

Display Layout:

显示布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


  <NumberPicker
      android:id="@+id/np1"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
       />
</LinearLayout>

Activity Layout:

活动布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="ale.demarco.ale005.SecondActivity$PlaceholderFragment" >

 <TextView
    android:id="@+id/Text2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Text2" 
    android:textSize="20sp"/>



<Button
    android:id="@+id/button2"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button2"
    android:onClick="button2"
    android:layout_gravity="right" />


</LinearLayout>

回答by Libin

Use picker.getValue()to get the current value of the picker.

使用picker.getValue()得到拾取的当前值。

When the Dialogbutton is checked , update the TextViewin Activitywith picked value

Dialog按钮被选中,更新TextViewActivity与钦点值

 builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int which) {
        int pickedValue = picker.getValue();
        // set your TextView id instead of R.id.textView1
        TextView textView = (TextView)findViewById(R.id.textView1);
        textView.setText(Integer.toString(pickedValue));
        return;
    } });

You can also add a listener to listen for the number changes like this

您还可以添加一个监听器来监听这样的数字变化

picker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
           // do your other stuff depends on the new value

        }
    });

回答by bschwagg

Make sure to add the final qualifier to your NumberPicker:
NumberPicker picker = (NumberPicker) v1.findViewById(R.id.np1);
to
final NumberPicker picker = (NumberPicker) v1.findViewById(R.id.np1);

确保最终预选赛添加到您的NumberPicker:
NumberPicker picker = (NumberPicker) v1.findViewById(R.id.np1);

final NumberPicker picker = (NumberPicker) v1.findViewById(R.id.np1);