简体   繁体   English

在EditText中输入字母时如何烘烤

[英]How to toast when letter is inputed in EditText

I am a beginner starting to design a simple app to keep track of golf scores. 我是一个初学者,开始设计一个简单的应用程序来跟踪高尔夫球得分。 I spent some time trying to figure out how to provide the user with a notification that they are unable to enter a letter under the score text box. 我花了一些时间试图弄清楚如何为用户提供一个通知,告知他们他们无法在得分文本框中输入字母。 I have tried using an array that contains the entire alphabet but have not been able to get it to work. 我尝试使用包含整个字母的数组,但无法使其正常工作。 I am currently now experimenting with InputFilter but I am not having any success; 我目前正在尝试使用InputFilter,但没有获得任何成功。 the app simply crashes when a letter has been entered. 输入字母后,应用只会崩溃。

    EditText input1 = (EditText)findViewById(R.id.scorePrompt);
    TextView output1 = (TextView)findViewById(R.id.textTotal);
    String blankCheck = input1.getText().toString(); //CHANGE INPUT IN scorePrompt TO STRING

    InputFilter filter = new InputFilter() {
        public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
            for (int i = start; i < end; i++) {
                if (Character.isLetter(source.charAt(i))) { // Accept only letter & digits ; otherwise just return
                    Toast.makeText(getApplicationContext(),"NO LETTERS",Toast.LENGTH_SHORT).show();
                    return "";
                }
            }
            return null;
        }
    };

I suspect that it is because I am not directly connecting the Inputfilter to my blankCheck, but I am unsure. 我怀疑这是因为我没有将Inputfilter直接连接到我的blankCheck,但是不确定。 Any help is appreciated! 任何帮助表示赞赏!

Often the answer you're looking for is a result of a problem that originates elsewhere. 通常,您要寻找的答案是源自其他地方的问题的结果。

If you'd like your users to input numbers rather than text, the best solution would be to specify an input type of "number" on the EditText field they are typing in. This will limit the keyboard input to only allow numbers. 如果您希望用户输入数字而不是文本,则最好的解决方案是在他们输入的EditText字段上将输入类型指定为“数字”。这会将键盘输入限制为仅允许数字。

<EditText
    android:inputType="number"
    .... other attributes
</EditText>

U can make use of Regex for this, please see the below code : 您可以为此使用正则表达式,请参见以下代码:

// Define a pattern you want to use as below. //如下定义您要使用的模式。 // I'm only supporting the below : //我只支持以下内容:

Pattern keyPattern = Pattern.compile("[0-9a-zA-Z,._]+");

yourEditText.addTextChangedListener(new TextWatcher() {
    @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) {
    if (!keyPattern .matcher(yourEditText.getText()).matches()) {
        Toast.makeText(getApplicationContext(),"Your Message",Toast.LENGTH_SHORT).show();
                    return "";
 }
});

Hope this helps!!! 希望这可以帮助!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM