How to add a character in EditText after user starts typing

后端 未结 3 1965
余生分开走
余生分开走 2021-01-26 16:54

I have an editText where a user inputs a phone number, but right when they click their first number, I want a \'+\' to appear in the beginning of the text. I have this code but

3条回答
  •  天命终不由人
    2021-01-26 17:20

    Use TextWatcher

    editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
    

    Implement it like this. You can also trick it to fit your needs

    editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_DONE) {
                // Specify your database function here.
                return true;
            }
            return false;
        }
    });
    

    Alternatively, you can use the OnEditorActionListener interface to avoid the anonymous inner class.

提交回复
热议问题