我有一个Edit Text
,其中的数字只能在0-59
之间,Edit Text
中不会输入任何其他数字,我尝试了text-watcher
但未获得成功。任何的想法?
12-26 14:59:39.715: E/AndroidRuntime(19494): FATAL EXCEPTION: main
12-26 14:59:39.715: E/AndroidRuntime(19494): java.lang.StackOverflowError
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.findViewTraversal(ViewGroup.java:2765)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewById(View.java:10442)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findViewShouldExist(View.java:4281)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.findUserSetNextFocus(View.java:4263)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.FocusFinder.findNextFocus(FocusFinder.java:66)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:569)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.ViewGroup.focusSearch(ViewGroup.java:571)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.View.focusSearch(View.java:4221)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.onCreateInputConnection(TextView.java:5378)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.inputmethod.InputMethodManager.startInputInner(InputMethodManager.java:977)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.view.inputmethod.InputMethodManager.restartInput(InputMethodManager.java:932)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2958)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494): at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494): at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494): at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494): at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2882)
12-26 14:59:39.715: E/AndroidRuntime(19494): at com.fishing.MyLocation$check_lat_degree.onTextChanged(MyLocation.java:236)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.sendOnTextChanged(TextView.java:7226)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:3045)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.java:2907)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.EditText.setText(EditText.java:90)
12-26 14:59:39.715: E/AndroidRuntime(19494): at android.widget.TextView.setText(TextView.j
答案 0 :(得分:15)
我喜欢这样做:
public class InputFilterMinMax implements InputFilter {
private int min;
private int max;
public InputFilterMinMax(int min, int max) {
this.min = min;
this.max = max;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
//noinspection EmptyCatchBlock
try {
int input = Integer.parseInt(dest.subSequence(0, dstart).toString() + source + dest.subSequence(dend, dest.length()));
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) { }
return "";
}
private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
}
然后在EditText上设置过滤器:
myEditText.setFilters(new InputFilter[]{new InputFilterMinMax(0, 59)});
答案 1 :(得分:6)
在XML中使用此代码仅允许编辑文本:
中的数字<EditText android:id="@+id/edit_text_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iputType="numbers" />
控制在EditText中输入的文本值:
edittext.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){
String strEnteredVal = edittext.getText().toString();
if(!strEnteredVal.equals("")){
int num=Integer.parseInt(strEnteredVal);
if(num<60){
edittext.setText(""+num);
}else{
edittext.setText("");
}
}
});
答案 2 :(得分:0)
首先将输入类型添加为数字并限制editText的输入数量。
editText.setInputType( InputType.TYPE_CLASS_NUMBER );
InputFilter[] FilterArray = new InputFilter[1];
FilterArray[0] = new InputFilter.LengthFilter(2);
editText.setFilters(FilterArray);
然后
添加TextWatcher
以检查用户输入的内容,如果值超过59,则显示警报或任何消息,该数字将小于60.
editText.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
String added_number = editText.getText().toString();
if (added_number.length != 0) {
int number = Integer.parseInt(added_number);
if (number > 59)
Toast.makeText(getApplicationContext, "Not more than 59", Toast.LENGTH_SHORT).show();
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
});
答案 3 :(得分:0)
void addAddTextChangeListener(final EditText et) {
et.addTextChangedListener(new TextWatcher() {
boolean textModified = false;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
String strEnteredVal = et.getText().toString();
if(!strEnteredVal.equals("") && !(strEnteredVal.length() < 2)) {
int num = Integer.parseInt(strEnteredVal);
if (!textModified) {
if (num > 60) {
et.setText("");
} else {
textModified = true;
et.setText("" + num);
et.setEnabled(false);
}
}
}
}
});
}
xml文件中的android:inputType="number"
条目中的和EditText
。
但是一旦获得正确的输入,此代码就会停止工作,因此您应该在此之后停用EditText
。