简体   繁体   English

EditText - 键入时更改文本

[英]EditText - change text while typing

I need to replace the text inside the EditText while typing : Example : if the user pressed "A" it would be stored into a buffer and on the EditText "D" is displayed instead (looks like he pressed "D"). 我需要在键入时替换EditText内的文本:示例:如果用户按下“A”,它将被存储到缓冲区中,而在EditText上显示“D”(看起来他按下了“D”)。 Now I can read the pressed character but I can't display any character in the et to avoid stackoverflow : 现在我可以读取按下的字符但是我不能在et中显示任何字符以避免stackoverflow:

final EditText et = (EditText) findViewById(R.id.editTexts);
    final TextView tv = (TextView) findViewById(R.id.textView2);

    et.addTextChangedListener(new TextWatcher()
    {
            public void afterTextChanged(Editable s){}
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            } 
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if(s.length() > 0) {
                    tv.setText(s.toString().substring(s.length()-1));
                    et.setText("");
                }
            }
    });

You can change it as required:: 你可以根据需要改变它::

public class SampleActivity extends Activity {
    TextWatcher tt = null;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final EditText et = (EditText) findViewById(R.id.editText1);
        final TextView tv = (TextView) findViewById(R.id.textView1);
        tt = new TextWatcher() {
           public void afterTextChanged(Editable s){
                et.setSelection(s.length());
           }
           public void beforeTextChanged(CharSequence s,int start,int count, int after){} 
           public void onTextChanged(CharSequence s, int start, int before, int count) {
               et.removeTextChangedListener(tt);
               et.setText(et.getText().toString().replace("A", "C"));
               et.addTextChangedListener(tt);
           }
       };
       et.addTextChangedListener(tt);
   }
}

In order to change the text interactively, you need to register a TextWatcher . 要以交互方式更改文本,您需要注册TextWatcher But trying to change the text inside the watcher creates further calls to the watcher. 但是尝试更改观察者内部的文本会进一步调用观察者。 My hack is to temporarily remove the watcher when I want to change the text, and re-register it right after 我的黑客是在我想要更改文本时临时删除观察者,然后立即重新注册

    mEditText.addTextChangedListener(new TextWatcher() {
        @Override public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
        @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { }
        @Override public void afterTextChanged(Editable editable) {
            mEditText.removeTextChangedListener(this);
            mEditText.setText(//TODO change whatever you like here);
            mEditText.setSelection(editable.length()); //moves the pointer to end
            mEditText.addTextChangedListener(this);
        }

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

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