简体   繁体   English

NumberPicker不适用于键盘

[英]NumberPicker doesn't work with keyboard

In activity_main.xml : activity_main.xml

<NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp" />

In MainActivity.java inside the oncreate : oncreate里面的MainActivity.java

NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
numberPicker1.setMinValue(1);
numberPicker1.setMaxValue(20);

When I edit the value with the keyboard, the value that I get programmatically doesn't change, but if I press + then - it works. 当我用键盘编辑值时,我以编程方式获得的值不会改变,但如果我按+然后-它可以工作。

How can I make it work without having to press + and - ? 如何在不按+-情况下使其工作?


EDIT : 编辑:

I created a new Project with the code of Melih Altıntaş and it displayed a whole new numberpicker!! 我用MelihAltıntaş的代码创建了一个新项目,它显示了一个全新的数字选择器! (The one where you slide with your finger and can't enter text). (用手指滑动但不能输入文字的那个)。 Then I compared with my real project and I saw android:theme="@android:style/Theme.NoTitleBar" . 然后我比较了我的真实项目,我看到了android:theme="@android:style/Theme.NoTitleBar" I added it in the new project and then the numberpicker became the one I was used to. 我在新项目中添加了它,然后numberpicker成为我习惯的那个。

如果有人想要一个简单的解决方法,只需在保存值之前添加以下代码。

numberPicker.clearFocus();

The NumberPicker wasn't designed for that interaction. NumberPicker不是为该交互而设计的。 When you change the value with the keyboard you're making changes directly to a widget from the NumberPicker and the widget itself doesn't see this change so this is why you end up with the last stored value. 当您使用键盘更改值时,您正在从NumberPicker直接更改窗口小部件,并且窗口小部件本身不会看到此更改,因此这就是您最终存储最后一个值的原因。 To get around this you'll need some hacky code, which isn't really recommended, because you'll need to access the underlying EditText (assuming that the phone maker didn't changed this in the first place): 要解决这个问题,你需要一些hacky代码,这不是真正推荐的,因为你需要访问底层的EditText (假设手机制造商并没有改变这一点):

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;
}

and then you'll use this code to set a TextWatcher on the found EditText to see when it's manually modified(and let the NumberPicker know about this change): 然后你将使用这段代码在找到的EditText上设置TextWatcher ,看看它何时被手动修改(并让NumberPicker知道这个变化):

EditText input = findInput(np);    
TextWatcher tw = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {}

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {}

        @Override
        public void afterTextChanged(Editable s) {
                if (s.toString().length() != 0) {
                    Integer value = Integer.parseInt(s.toString());
                    if (value >= np.getMinValue()) {
                        np.setValue(value);
                    }
                }
        }
    };
input.addTextChangedListener(tw);

Another option would be to make your own implementation of the NumberPicker widget and insert the functionality you target. 另一种选择是自己实现NumberPicker小部件并插入您目标的功能。

您必须按“ Done按钮才能以编程方式更改值。

NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker1);
String[] nums = new String[20];
for(int i=0; i<nums.length; i++)
       nums[i] = Integer.toString(i);

np.setMinValue(1);
np.setMaxValue(20);
np.setWrapSelectorWheel(false);
np.setDisplayedValues(nums);
np.setValue(1);

I see that you already have an answer, however this may be useful to others. 我看到你已经有了答案,但这可能对其他人有用。 You can extend android.widget.NumberPicker and make your own where you can configure the EditText. 您可以扩展android.widget.NumberPicker,并在您可以配置EditText的地方自行创建。 As you type on the soft keyboard, the value of your NumberPicker is immediately set to the value you're typing. 当您在软键盘上键入时,NumberPicker的值会立即设置为您键入的值。

public class MyNumberPicker extends android.widget.NumberPicker {

       private EditText eText;

       public MyNumberPicker(Context context, AttributeSet attrs) {
        super(context, attrs);
       }


       @Override
       public void addView(View child) {
         super.addView(child);
         initEditText(child);
       }

       @Override
       public void addView(View child, int index, android.view.ViewGroup.LayoutParams params) {
         super.addView(child, index, params);
         initEditText(child);
       }

       @Override
       public void addView(View child, android.view.ViewGroup.LayoutParams params) {
         super.addView(child, params);
         initEditText(child);
       }

       public void initEditText(View view){
           if(view instanceof EditText){
               EditText eText = (EditText) view;
               eText.addTextChangedListener(new TextWatcher(){

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                    try{
                        int num = Integer.parseInt(s.toString());
                        setValue(num); //this line changes the value of your numberpicker
                    }catch(NumberFormatException E){
                            //do your catching here
                    }   
                }});     
             }
          }
     }

In your XML-file you put the following: 在XML文件中,您输入以下内容:

<com.mypackage.MyNumberPicker
 android:id="@+id/numberPicker1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content" />

the reason why it doesn't work it's because if you look at the implementation of setValue in NumberPicker, it calls another method setValueInternal(int, boolean) which takes 2 parameters the new value and of course the bugger boolean which is representing whether it should notify the UI of any change or not. 它之所以不起作用,是因为如果你看一下NumberPicker中setValue的实现,它会调用另一个方法setValueInternal(int,boolean),它接受2个参数的新值,当然还有bugger boolean,它表示是否应该通知UI任何更改。 And guess what, in setValue the boolean goes always with false. 猜猜看,在setValue中,布尔值始终为false。 Godammit. Godammit。 But there is one more trick I see that the validateInputTextView actually does it correctly, and it invokes when the edit text has no focus. 但是还有一个技巧我看到validateInputTextView实际上正确地执行了它,并且它在编辑文本没有焦点时调用。 maybe at the right time clearing the focus will do what is supposed to do. 也许在正确的时间清除焦点将完成应该做的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM