如何在对话框中使用数字选择器

我想使用数字选择器来获取用户的折扣百分比。 一旦用户输入销售价格,我想要一个对话框来询问折扣百分比。 我找不到在对话框中集成numberpicker的方法。

  • Android - 自定义对话框 - 无法从EditText获取文本
  • 使用Dialog自定义屏幕变暗
  • 单击按钮时打开对话框
  • Android中“FLAG_BLUR_BEHIND”的替代?
  • 对话问题:在添加内容之前必须调用requestFeature()
  • BottomSheetDialog / BottomSheetDialogFragment - 使用哪些以及如何使用?
  • Android:触摸时关闭对话框窗口
  • 带有数字选择器的Android PreferenceActivity对话框
  • 请尝试以下代码:

      RelativeLayout linearLayout = new RelativeLayout(mContext); final NumberPicker aNumberPicker = new NumberPicker(mContext); aNumberPicker.setMaxValue(50); aNumberPicker.setMinValue(1); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(50, 50); RelativeLayout.LayoutParams numPicerParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); numPicerParams.addRule(RelativeLayout.CENTER_HORIZONTAL); linearLayout.setLayoutParams(params); linearLayout.addView(aNumberPicker,numPicerParams); AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mContext); alertDialogBuilder.setTitle("Select the number"); alertDialogBuilder.setView(linearLayout); alertDialogBuilder .setCancelable(false) .setPositiveButton("Ok", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { Log.e("","New Quantity Value : "+ aNumberPicker.getValue()); } }) .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); 

    这是其他答案的较短变化

     NumberPicker picker = new NumberPicker(context); picker.setMinValue(1); picker.setMaxValue(50); FrameLayout layout = new FrameLayout(context); layout.addView(picker, new FrameLayout.LayoutParams( FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER)); new AlertDialog.Builder(context) .setView(layout) .setPositiveButton(android.R.string.ok, (dialogInterface, i) -> { // do something with picker.getValue() picker.getValue(); }) .setNegativeButton(android.R.string.cancel, null) .show(); 

    创建一个实现onCreateDialog方法的DialogFragment,该方法返回包含NumberPicker视图的警告对话框。 在活动中实例化对话框片段,您需要在该活动中显示数字选择器对话框并显示它。

    使主机活动实现NumberPicker.OnValueChangeListener接口,以便对话框中的正面和负面按钮单击事件处理程序可以通过调用接口onValueChange方法从NumberPicker传递当前值。

    有关更多信息,请参阅http://www.zoftino.com/android-numberpicker-dialog-example 。

     public class NumberPickerDialog extends DialogFragment { private NumberPicker.OnValueChangeListener valueChangeListener; @Override public Dialog onCreateDialog(Bundle savedInstanceState) { final NumberPicker numberPicker = new NumberPicker(getActivity()); numberPicker.setMinValue(20); numberPicker.setMaxValue(60); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle("Choose Value"); builder.setMessage("Choose a number :"); builder.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { valueChangeListener.onValueChange(numberPicker, numberPicker.getValue(), numberPicker.getValue()); } }); builder.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { valueChangeListener.onValueChange(numberPicker, numberPicker.getValue(), numberPicker.getValue()); } }); builder.setView(numberPicker); return builder.create(); } public NumberPicker.OnValueChangeListener getValueChangeListener() { return valueChangeListener; } public void setValueChangeListener(NumberPicker.OnValueChangeListener valueChangeListener) { this.valueChangeListener = valueChangeListener; } }