how to use number picker with dialog

后端 未结 4 1385
野的像风
野的像风 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:56

    I have made a small demo of NumberPicker. This may not be perfect but you can use and modify the same.

    Use a custom dialog and set the number picker.

    More info @

    http://developer.android.com/reference/android/widget/NumberPicker.html

    public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener
    {
        private TextView tv;
        static Dialog d ;
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.textView1);
            Button b = (Button) findViewById(R.id.button11);// on click of button display the dialog
             b.setOnClickListener(new OnClickListener()
             {
    
                @Override
                public void onClick(View v) {
                     show();
                }
                });
               }
        @Override
        public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
    
             Log.i("value is",""+newVal);
    
         }
    
        public void show()
        {
    
             final Dialog d = new Dialog(MainActivity.this);
             d.setTitle("NumberPicker");
             d.setContentView(R.layout.dialog);
             Button b1 = (Button) d.findViewById(R.id.button1);
             Button b2 = (Button) d.findViewById(R.id.button2);
             final NumberPicker np = (NumberPicker) d.findViewById(R.id.numberPicker1);
             np.setMaxValue(100); // max value 100
             np.setMinValue(0);   // min value 0
             np.setWrapSelectorWheel(false);
             np.setOnValueChangedListener(this);
             b1.setOnClickListener(new OnClickListener()
             {
              @Override
              public void onClick(View v) {
                  tv.setText(String.valueOf(np.getValue())); //set the value to textview
                  d.dismiss();
               }    
              });
             b2.setOnClickListener(new OnClickListener()
             {
              @Override
              public void onClick(View v) {
                  d.dismiss(); // dismiss the dialog
               }    
              });
           d.show();
    
    
        }
    }
    

    activity_main.xml

    
    
        
    
        

    dialog.xml

    
    
        
    
        

    Snap shot

    enter image description here

提交回复
热议问题