在 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" />
在 MainActivity.java
在的OnCreate
:
NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
numberPicker1.setMinValue(1);
numberPicker1.setMaxValue(20);
当我编辑值与键盘,我得到编程的价值不会改变,但如果我preSS +
然后 -
它的工作原理
When I edit the value with the keyboard, the value that I get programmatically doesn't change, but if I press +
then -
it works.
怎样才能使它不必工作preSS +
和 -
How can I make it work without having to press +
and -
?
编辑:
我创建了一个新的项目与MelihAltıntaş的code,它显示了一个全新的numberpicker! (就是那间你滑动的手指,不能输入文字)。然后,我用我的真正的项目相比,我看到安卓主题=@安卓风格/ Theme.NoTitleBar
。我把它添加了新的项目,然后numberpicker变成了一个我一直在用。
I created a new Project with the code of Melih Altıntaş and it displayed a whole new numberpicker!! (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"
. I added it in the new project and then the numberpicker became the one I was used to.
在 NumberPicker
不是为这种互动。当您更改使用键盘的价值你直接更改为从插件的 NumberPicker
和小部件本身并没有看到这种变化,所以这是为什么你最终与上次存储的值。为了解决这个问题,你需要一些哈克code,这并不推荐使用,因为你需要访问下划线的EditText
(假设手机制造商并没有改变这个摆在首位):
The NumberPicker
wasn't designed for that interaction. 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. To get around this you'll need some hacky code, which isn't really recommended, because you'll need to access the underlining EditText
(assuming that the phone maker didn't changed this in the first place):
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;
}
,然后你会使用这个code设置 TextWatcher
上找到的EditText
看当它的手动修改(并让 NumberPicker
了解这种变化):
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 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
控件并插入你的目标的功能。
Another option would be to make your own implementation of the NumberPicker
widget and insert the functionality you target.
上一篇:我怎么能检查哪些摇篮Android插件版本是在我的项目中使用?我的、是在、摇篮、插件
下一篇:我怎么办?答联想IdeaPad S6000连接到Windows 7(64位)连接到、IdeaPad、Windows