NumberPicker不适用于键盘
在activity_main.xml
:
在oncreate
里面的MainActivity.java
:
NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1); numberPicker1.setMinValue(1); numberPicker1.setMaxValue(20);
当我用键盘编辑值时,我以编程方式获得的值不会改变,但如果我按+
然后-
它可以工作。
如何在不按+
和-
情况下使其工作?
编辑:
我用MelihAltıntaş的代码创建了一个新项目,它显示了一个全新的数字选择! (用手指滑动但不能输入文字的那个)。 然后我比较了我的真实项目,我看到了android:theme="@android:style/Theme.NoTitleBar"
。 我在新项目中添加了它,然后numberpicker成为我习惯的那个。
NumberPicker
不是为该交互而设计的。 当您使用键盘更改值时,您正在从NumberPicker
直接更改窗口小部件,并且窗口小部件本身不会看到此更改,因此这就是您最终存储最后一个值的原因。 为了解决这个问题,你需要一些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; }
然后你将使用这段代码在找到的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);
另一个选择是创建自己的NumberPicker
小部件实现并插入您目标的function。
如果有人想要一个简单的解决方法,只需在保存值之前添加以下代码。
numberPicker.clearFocus();
您必须按“ Done
按钮才能以编程方式更改值。
NumberPicker np = (NumberPicker) findViewById(R.id.numberPicker1); String[] nums = new String[20]; for(int i=0; i
我看到你已经有了答案,但这可能对其他人有用。 您可以扩展android.widget.NumberPicker,并在您可以配置EditText的地方自行创建。 当您在软键盘上键入时,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 } }}); } } }
在您的XML文件中,您输入以下内容:
它之所以不起作用,是因为如果你看一下NumberPicker中setValue的实现,它会调用另一个方法setValueInternal(int,boolean),它接受2个参数的新值,当然还有bugger boolean,它表示是否应该通知UI任何更改。 猜猜看,在setValue中,布尔值始终为false。 Godammit。 但是还有一个技巧我看到validateInputTextView实际上正确地执行了它,并且它在编辑文本没有焦点时调用。 也许在正确的时间清除焦点将做应该做的事情。
- 突出显示TextView中的文本行,包括所有宽度
- java.lang.IllegalArgumentException:找不到片段FmMenu的id 0x7f090047(“项目名称”:id / content)的视图
- 通过摄像头拍照并以bytearray的forms发送到服务器
- onPictureTaken()出错,问题是无法将RGB转换为GRAY,程序停止从cvtcolor()函数运行。
- 错误“由以下引起:java.lang.ClassNotFoundException:”
- android – 生成随机日期和时间
- JavaScript不适用于ICS
- Android上两个应用程序之间的套接字通信
- 转换时Gson忽略了我的字段