在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);
当我使用键盘编辑值时,我以编程方式获得的值不会更改,但是如果我按+
,那么-
就会有效。
如何在不必按+
和-
?
编辑:
我使用MelihAltıntaş的代码创建了一个新项目,它显示了一个全新的数字选择器!! (用手指滑动但不能输入文字的那个)。然后我与我的真实项目进行了比较,我看到了android:theme="@android:style/Theme.NoTitleBar"
。我在新项目中添加了它,然后numberpicker成为我习惯的那个。
答案 0 :(得分:16)
如果有人想要一个简单的解决方法,只需在保存值之前添加以下代码。
numberPicker.clearFocus();
答案 1 :(得分:9)
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;
}
然后您将使用此代码在找到的TextWatcher
上设置EditText
以查看何时手动修改(并让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
窗口小部件并插入您定位的功能。
答案 2 :(得分:1)
您必须按Done
按钮才能以编程方式更改值。
答案 3 :(得分:1)
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);
答案 4 :(得分:1)
我看到你已经有了答案,但这可能对其他人有用。您可以扩展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文件中,您输入以下内容:
<com.mypackage.MyNumberPicker
android:id="@+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
答案 5 :(得分:0)
它之所以不起作用,是因为如果你看一下NumberPicker中setValue的实现,它会调用另一个方法setValueInternal(int,boolean),它接受2个参数的新值,当然还有代表的bugger boolean是否应通知UI任何变更。猜猜看,在setValue中,布尔值始终为false。 Godammit。但是还有一个技巧我看到validateInputTextView实际上正确地执行了它,并且它在编辑文本没有焦点时调用。也许在正确的时间清除焦点将完成应该做的事情。