android 更改edittext内容,Android如何实时更改edittext的内容
為此使用TextWatcher。設置它的EditText用戶類型:
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String value = s.toString();
// Perform computations using this string
// For example: parse the value to an Integer and use this value
// Set the computed value to the other EditText
myEditText2.setText(computedValue);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
}
});
編輯1:
檢查空字符串"":
myEditText1.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
String value = s.toString();
if (value.equals("")) {
myEditText1.setText("0");
// You may not need this line, because "myEditText1.setText("0")" will
// trigger this method again and go to else block, where, if your code is set up
// correctly, myEditText2 will get the value 0. So, try without the next line
// and if it doesn't work, put it back.
myEditText2.setText("0");
} else {
// Perform computations using this string
// For example: parse the value to an Integer and use this value
// Set the computed value to the other EditText
myEditText2.setText(computedValue);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count){
}
});
總結
以上是生活随笔為你收集整理的android 更改edittext内容,Android如何实时更改edittext的内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android vcard解析代码,An
- 下一篇: 沉寂两年宣布回归!前途汽车推“三新”规划