将edittext值设置为零(在为空时),并在进行任何输入时将其删除为零 [英] setting edittext value to zero when empty and removing zero when any entry is made
本文介绍了将edittext值设置为零(在为空时),并在进行任何输入时将其删除为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个用于显示数学函数结果的edittext字段.我有两个问题.我想在两种情况下将文本编辑为零值:第一个:我希望编辑文本为空时自动将其变为零
I have two edittext fields that i am using to display results of mathematical functions. I have two problems. I want to edit text to have a value of zero in two scenarios: 1st: I want the edit text value to automatically become zero if the edit text is empty
第二:如果用户删除字段值直到编辑文本为空.
2nd: if a user erases field values till the edit text is empty.
更新:我可以用setText()或android:text ="0"进行排序的第一个问题,但是当尝试解决第二个问题时,它就很棘手
UPDATE: The 1st problem I can sort with a setText() or android:text="0" but when trying to also solve the 2nd it becomes tricky
推荐答案
尝试如下操作:
final EditText et = new EditText(this);
et.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) {
}
@Override
public void afterTextChanged(Editable s) {
if(et.getText().length() == 0){
et.setText("0");
}
}
});
这篇关于将edittext值设置为零(在为空时),并在进行任何输入时将其删除为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文