简体   繁体   English

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

[英]How to make Number Picker Dialog?

My Requirement is like whenever user select any number from number picker on that time that number should be printed on some text view and second time whenever user opens dialog again than previously selected number should be shown on that dialog 我的要求就像每次用户从号码选择器中选择任何号码时,该号码应打印在某个文本视图上,而第二次用户再次打开对话框时,该对话框中将显示先前选择的号码

    ...
    ...

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

            }
        });
}

}

here's the number_picker.xml 这是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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM