且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

限制Android的NumberPicker到数字键盘输入数字(不包括alpha键盘)

更新时间:2023-10-03 18:50:04

我已经成功地做​​到了这一点,从大量举债@LuksProg's有用的答案的另一个问题。基本的想法是要搜索的NumberPicker的的EditText组件,然后向分配输入类型为数字。首先添加这个方法(再次感谢@LuksProg):

I have successfully achieved this, borrowing heavily from @LuksProg's helpful answer to another question. The basic idea is to search for the EditText component of the NumberPicker and then to assign the input type as numeric. First add this method (thanks again to @LuksProg):

private EditText findInput(ViewGroup np) {
    int count = np.getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = np.getChildAt(i);
        if (child instanceof ViewGroup) {
            findInput((ViewGroup) child);
        } else if (child instanceof EditText) {
            return (EditText) child;
        }
    }
    return null;
}

于是,在我的活动的onCreate()方法,我称之为,并设置输入类型:

Then, in my Activity's onCreate() method I call this and set the input type:

np.setMinValue(0);
np.setMaxValue(intensityStrings.length-1);
EditText input = findInput(np);    
input.setInputType(InputType.TYPE_CLASS_NUMBER);

这奏效了我!