【问题标题】:Android NumberPicker responseAndroid NumberPicker 响应
【发布时间】:2014-06-05 11:47:42
【问题描述】:

我已编译此代码以获得 NumberPicker。现在点击 n1 TextView,我看到 NumberPicker,但如果我点击 OK 按钮,数字不会被存储。

有什么想法吗?

TextView n1 = (TextView) findViewById(R.id.n1);
TextView n2 = (TextView) findViewById(R.id.n2);
TextView n3 = (TextView) findViewById(R.id.n3);
TextView n4 = (TextView) findViewById(R.id.n4);
TextView n5 = (TextView) findViewById(R.id.n5);
TextView n6 = (TextView) findViewById(R.id.n6);
TextView n7 = (TextView) findViewById(R.id.n7);
TextView n8 = (TextView) findViewById(R.id.n8);
TextView n9 = (TextView) findViewById(R.id.n9);



AlertDialog.Builder builder =  new AlertDialog.Builder(this);
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;
    } });
View view = getLayoutInflater().inflate(R.layout.number_picker_activity, null);
builder.setView (view);

final AlertDialog dialog = builder.create ();
NumberPicker picker = (NumberPicker) view.findViewById(R.id.rotella);
picker.setMinValue(1);
picker.setMaxValue(60);
picker.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    n1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.show();

        }
    });

【问题讨论】:

  • 你没有实现任何东西来存储一个值。您应该使用 sharedPreferences 并通过 picker.getValue(); 获取值;
  • 我认为...但是如何?

标签: android numbers numberpicker


【解决方案1】:

您的代码所做的只是在您单击第一个文本视图时提醒一个对话框。这就是你如何使用数字选择器

主Activity.java

public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener
{
    private static TextView tv;
    static Dialog d ;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.textView1);
        Button b = (Button) findViewById(R.id.button11);
         b.setOnClickListener(new OnClickListener()
         {

            @Override
            public void onClick(View v) {
                 show();
            }
            });
           }
     @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {

         Log.i("value is",""+newVal);

     }

    public void show()
    {

         final Dialog d = new Dialog(MainActivity.this);
         d.setTitle("NumberPicker");
         d.setContentView(R.layout.dialog);
         Button b1 = (Button) d.findViewById(R.id.button1);
         Button b2 = (Button) d.findViewById(R.id.button2);
         final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
         np.setMaxValue(100);
         np.setMinValue(0);
         np.setWrapSelectorWheel(false);
         np.setOnValueChangedListener(this);
         b1.setOnClickListener(new OnClickListener()
         {
          @Override
          public void onClick(View v) {
              tv.setText(String.valueOf(np.getValue()));
              d.dismiss();
           }    
          });
         b2.setOnClickListener(new OnClickListener()
         {
          @Override
          public void onClick(View v) {
              d.dismiss();
           }    
          });
       d.show();


    }
}

activity_main.xml

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/button11"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Open" />

</RelativeLayout>

dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/numberPicker1"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="98dp"
        android:layout_toRightOf="@+id/numberPicker1"
        android:text="Cancel" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_marginRight="16dp"
        android:layout_toLeftOf="@+id/numberPicker1"
        android:text="Set" />

</RelativeLayout>

在 res/values/dimens.xml 下

<resources>

    <!-- Default screen margins, per the Android Design guidelines. -->
    <dimen name="activity_horizontal_margin">16dp</dimen>
    <dimen name="activity_vertical_margin">16dp</dimen>

</resources>

感谢Raghunandann 提供此示例

【讨论】:

  • 嗨奥古斯都,现在我在对话框下插入。显示:String numero = String.valueOf(picker.getValue()); n1.setText(numero);
  • 对不起!我不明白。你的意思是它有效!?如果它对您有帮助,您可以投票并接受它,以便其他有类似问题的人可以看到..
猜你喜欢
  • 2013-05-11
  • 2014-01-24
  • 2016-09-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2014-11-19
  • 2015-08-09
  • 2015-09-10
相关资源
最近更新 更多