我看到您已经有了答案,但这可能对其他人有用。您可以扩展 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" />