如果用户首先输入特定数字,如何获取编辑文本仅接受短信

时间:2016-07-09 19:14:25

标签: android android-edittext

你如何得到它,以便编辑文本只接受文本,如果用户有一个K进入开始 喜欢

  1. KV1096
  2. K0024

1 个答案:

答案 0 :(得分:1)

在android中,有一个用于文本更改的监听器:

EditText mEditText = (EditText) findViewById(R.id.my_edit_text);

//now you can add the listener here:
mEditText.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) {
             // here get the value entered by user and get the character
             //at index 0; 
             String value = "what the user has entered".charAt(0)
             //now use an if statement here to see if it is your required value (K); if not, set EditText to disabled - not active;
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });

我希望这有助于您开始使用!