how to use number picker with dialog

后端 未结 4 1369
野的像风
野的像风 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条回答
  •  有刺的猬
    2020-12-03 05:45

    Please try the following code:

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

提交回复
热议问题