简体   繁体   English

如何验证Android虚拟键盘上的键?

[英]How to validate keys on the virtual keyboard on Android?

I want to have a toast message to display "Enter numeric characters only" whenever the user presses a non-numeric key on the virtual keyboard. 我希望每当用户在虚拟键盘上按下非数字键时,都显示一条Toast消息,以显示“仅输入数字字符”。 Any clue? 有什么线索吗?

You could set up a TextWatcher and process every character using the onTextChanged() event while the user types and ignore the unwanted characters as the user types. 您可以设置TextWatcher并在用户键入时使用onTextChanged()事件处理每个字符,并在用户键入时忽略不需要的字符。

Its not good UI design to keep popping up messages to the user on every keystroke, instead you could just let them know in the tutorial or introduction. 每次击键时都不断向用户弹出消息的UI设计不是很好,相反,您可以在教程或简介中让他们知道。

or use android:inputType="number" in your xml file. 或在您的xml文件中使用android:inputType =“ number”。 This will make it so that you can only enter numbers. 这样可以使您只能输入数字。 See "Input Method Framework" in Mark Murphy's book, or search on the developer site. 请参阅Mark Murphy的书中的“输入法框架”,或在开发人员站点上搜索。

As there isn't an event available for the virtual keyboard, i have found a way around to validate the keys by using the TextChanged event, Here's how i did it: 由于虚拟键盘没有可用的事件,因此我找到了一种使用TextChanged事件来验证按键的方法,这是我的方法:

import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class ShowKeypad extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        EditText emailTxt = (EditText) findViewById(R.id.editText);

        emailTxt.addTextChangedListener(new TextWatcher()
        {
                public void  afterTextChanged (Editable s){ 
                        Log.d("seachScreen", "afterTextChanged"); 
                } 
                public void  beforeTextChanged  (CharSequence s, int start, int 
                        count, int after)
                { 
                        Log.d("seachScreen", "beforeTextChanged"); 
                } 
                public void  onTextChanged  (CharSequence s, int start, int before, 
                        int count) 
                { 
                        Log.d("seachScreen", s.toString()); 
                }

        final TextView tv = (TextView)findViewById(R.id.tv);
        /*emailTxt.setOnKeyListener(new OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) { 
                //Log.i("Key Value", String.valueOf(keyCode)); 
                //Toast.makeText(ShowKeypad.this, String.valueOf(keyCode), Toast.LENGTH_SHORT);
                Log.i("Key Value:", String.valueOf(keyCode));

                return true;
                }; });*/

        }); 
    }

}

Is there any better way to validate? 有没有更好的验证方法? Please give your feedback. 请提供您的反馈。

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

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