How to create a number picker dialog?

前端 未结 5 2216
误落风尘
误落风尘 2020-11-28 05:40

I want to be able to create a Dialog that allows the user to pick a number from a specified range.

I know that there are existing widgets(like those from quietlycod

5条回答
  •  抹茶落季
    2020-11-28 05:59

    To show NumberPicker in AlertDialog use this code :

    final AlertDialog.Builder d = new AlertDialog.Builder(context);
    LayoutInflater inflater = this.getLayoutInflater();
    View dialogView = inflater.inflate(R.layout.number_picker_dialog, null);
    d.setTitle("Title");
    d.setMessage("Message");
    d.setView(dialogView);
    final NumberPicker numberPicker = (NumberPicker) dialogView.findViewById(R.id.dialog_number_picker);
    numberPicker.setMaxValue(50);
    numberPicker.setMinValue(1);
    numberPicker.setWrapSelectorWheel(false);
    numberPicker.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
        @Override
        public void onValueChange(NumberPicker numberPicker, int i, int i1) {
            Log.d(TAG, "onValueChange: ");
        }
    });
    d.setPositiveButton("Done", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            Log.d(TAG, "onClick: " + numberPicker.getValue());
        }
    });
    d.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
        }
    });
    AlertDialog alertDialog = d.create();
    alertDialog.show();
    

    number_picker_dialog.xml

    
    
    
    
    

提交回复
热议问题