Android: How to use NumberPickerDialog

假如想象 提交于 2019-12-04 21:09:56

Got it working eventually. There's an example in com.quietlycoding.android.picker.Picker, but I've found that the dialog doesn't set the dimming properly, blacking out the whole Activity in the background while it's in view.

I worked around this by simply creating an AlertDialog in the usual way, and then just sticking a NumberPicker widget into setView():

LayoutInflater inflater = (LayoutInflater)
    getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View npView = inflater.inflate(R.layout.number_picker_pref, null);
    return new AlertDialog.Builder(this)
        .setTitle("Text Size:")
        .setView(npView)
        .setPositiveButton(R.string.dialog_ok,
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                }
            })
            .setNegativeButton(R.string.dialog_cancel,
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                    }
                })
            .create();

Make sure to copy number_picker_pref.xml from the numberpicker project to res/layout in your own project.

aeracode

Look at this (and optionaly this: Creating dialogs).

That should be much simpler if only you do this:

  1. Add NumberPicker to you layout
  2. Add this code on your Activity

        //charger le NumberPicker
        npicker = (NumberPicker) findViewById(R.id.picker);
        // Set intervalle
        npicker.setRange(1, pages.size());
        // Set la valeur actuelle
        npicker.setCurrent(1);
    
        npicker.setOnChangeListener(new OnChangedListener() {               
            @Override
            public void onChanged(NumberPicker picker, int oldVal, int newVal) {
                // TODO Auto-generated method stub
                Log.e("Log Change event","oldVal: "+oldVal+"//newVal: "+newVal);
            }
        });
    
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!