如何在更改 EditText 的文本时更改 TextView 的文本? [英] How to change TextView's text on change of EditText's text?
问题描述
我有一个 EditText
框,用户可以在其中输入内容.当用户输入文本时,我需要在 TextView
(位于 EditText 下方)中显示输入文本.
I have an EditText
box where the user can enter input. I need to display the input text in a TextView
(which is placed below the EditText) while the user inputting the text.
示例:
如果用户在 EditText 框中输入任何字符,我需要在 TextView 中显示相同的字符.同样,如果用户从 EditText 中删除任何字符,我需要从 TextView 中删除相同的字符.(最终,我想在更改 EditText 的文本时更改 TextView 的文本).我希望我的要求现在很清楚.我怎样才能做到这一点?请指导我.
If the user is entering any characters in EditText box, I need to display the same characters in TextView. In the same way, If the user deletes any character from EditText, I need to remove the same character from TextView. (Ultimately, I want to change TextView's text on change of EditText's text). I hope my requirement is clear now. How can I achieve this? Please guide me.
推荐答案
将 TextWatcher 添加到您的 Edittext.在 afterTextChanged()
中进行操作.http://developer.android.com/reference/android/text/TextWatcher.html
Add TextWatcher to your Edittext. in afterTextChanged()
do your operation.
http://developer.android.com/reference/android/text/TextWatcher.html
TextWatcher inputTextWatcher = new TextWatcher() {
public void afterTextChanged(Editable s) {
textview.setText(s.toString());
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
};
editText.addTextChangedListener(inputTextWatcher);
这篇关于如何在更改 EditText 的文本时更改 TextView 的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!