gpt4 book ai didi

android - 如何在 Android 的编辑文本中将文本限制在 0-59 之间?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:10:45 25 4
gpt4 key购买 nike

我有一个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

最佳答案

这是我喜欢这样做的方式:

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)});

关于android - 如何在 Android 的编辑文本中将文本限制在 0-59 之间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14036674/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com