android - 如何制作数字选择器对话框?

标签 android android-dialog

我的要求就像每当用户从数字选择器中选择任何数字时,该数字应该打印在一些 TextView 上,第二次每当用户再次打开对话框时,之前选择的数字应该显示在该对话框上

    ...
    ...

    view = (TextView) findViewById(R.id.textView7);

    LinearLayout L = (LinearLayout) findViewById(R.id.linearLayout5);
    L.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            showdialog();
        }
    });
}

@Override
public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
         Log.i("value is",""+newVal);
}

public void showdialog()
{
    Dialog dialog = new Dialog(NameActivity.this);
    View npView = getLayoutInflater().inflate(R.layout.multidialog, null);
    final NumberPicker firPicker = 
            (NumberPicker) npView.findViewById(R.id.numberPicker1);
    firPicker.setMaxValue(9);
    firPicker.setMinValue(0);
    final NumberPicker secPicker = 
            (NumberPicker) npView.findViewById(R.id.numberPicker2);
    secPicker.setMaxValue(9);
    secPicker.setMinValue(0);
    final NumberPicker tirPicker = 
            (NumberPicker) npView.findViewById(R.id.numberPicker3);
    tirPicker.setMaxValue(9);
    tirPicker.setMinValue(0);

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("title");
    builder.setView(npView);
    builder.setPositiveButton("Set",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            view.setText(String.valueOf(firPicker.getValue() *100 
                    + secPicker.getValue() *10 + tirPicker.getValue()));
        }
    });

    builder.setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
        }
    });
    dialog = builder.create();
    dialog.show();
}

最佳答案

public class sample extends Activity{

NumberPicker MyNumPicker1, MyNumPicker2, MyNumPicker3;
TextView txtMyText;
AlertDialog alertdialog;

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

    txtMyText = (TextView) findViewById(R.id.txtMyText);
    txtMyText.setText("5");

    txtMyText.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View v1 = inflater.inflate(R.layout.numpicker, null);
                MyNumPicker1 = (NumberPicker) v1.findViewById(R.id.MyNunPicker1);
                MyNumPicker2 = (NumberPicker) v1.findViewById(R.id.MyNunPicker2);
                MyNumPicker3 = (NumberPicker) v1.findViewById(R.id.MyNunPicker3);

                MyNumPicker1.setMaxValue(20);
                MyNumPicker1.setMinValue(1);
                MyNumPicker1.setValue(Integer.parseInt(txtMyText.getText().toString()));
                MyNumPicker1.setWrapSelectorWheel(true);

                MyNumPicker2.setMaxValue(20);
                MyNumPicker2.setMinValue(1);
                MyNumPicker2.setValue(Integer.parseInt(txtMyText.getText().toString()));
                MyNumPicker2.setWrapSelectorWheel(true);

                MyNumPicker3.setMaxValue(20);
                MyNumPicker3.setMinValue(1);
                MyNumPicker3.setValue(Integer.parseInt(txtMyText.getText().toString()));
                MyNumPicker3.setWrapSelectorWheel(true);


                AlertDialog.Builder builder = new AlertDialog.Builder(sample.this);

                builder.setView( v1 );
                builder.setTitle("Select Number");
                builder.setPositiveButton("Set", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        int NumVal1 = MyNumPicker1.getValue();
                        int NumVal2 = MyNumPicker2.getValue();
                        int NumVal3 = MyNumPicker3.getValue();

                        txtMyText.setText(""+ ( (NumVal1 * 100) + (NumVal2 * 10) + NumVal3) );
                    }
                });

                builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertdialog.dismiss();

                    }
                });

                alertdialog = builder.create();
                alertdialog.show();

            }
        });
}

}

这是 number_picker.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<NumberPicker
    android:id="@+id/MyNunPicker2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<NumberPicker
    android:id="@+id/MyNunPicker3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/MyNunPicker2" />

<NumberPicker
    android:id="@+id/MyNunPicker1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/MyNunPicker2"/>

</RelativeLayout>

关于android - 如何制作数字选择器对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22188552/

相关文章:

java - 安卓 :UI Thread Blocked

android - AsyncTask 中 ListItem 的 AlertDialog onclick

java - 向警报对话框添加圆角

Android 使用 ArrayList 中的值创建 AlertDialog?

java - 自定义 setMessage 对话框 TextView 无法转换为 java.lang.CharSequence

android - 当我尝试显示对话框时,它给我强制关闭错误

java - 操作栏在 Lollipop 之前的设备中透明

用于显示图形(节点和边,二维)的 Android 组件?

java - 如何发送创建 JSON 字符串并将其从服务器发送到 android?

Android:单击相机中的图像并在对话框中显示图像