我看到你已經有了答案,但這可能對別人有用。您可以擴展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" />
我有同樣的問題,有沒有人提出一個更好的答案呢? –