how to use number picker with dialog

后端 未结 4 1376
野的像风
野的像风 2020-12-03 05:16

I want to use a number picker for the purpose of getting the discount percentage from the user. once the user enters the sale price, i want a dialog box to appear asking for

4条回答
  •  -上瘾入骨i
    2020-12-03 05:56

    This is a shorter variation of the other answers

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

提交回复
热议问题