簡體   English   中英

如何制作數字選擇器對話框?

[英]How to make Number Picker 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();

            }
        });
}

}

這是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