gpt4 book ai didi

java - Android EditText onChangeListener 函数

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:07 24 4
gpt4 key购买 nike

我有以下代码:

nameOfInf.setOnFocusChangeListener(new OnFocusChangeListener() {
if (strTollAmount.length() > 0) {
nameOfInf.setBackgroundColor(getResources().getColor(android.R.color.white));
nameOfInf.setTextColor(getResources().getColor(android.R.color.black));
}
});
tollAmount.setOnFocusChangeListener(new OnFocusChangeListener() {
if (strInfName.length() > 0) {
tollAmount.setBackgroundColor(getResources().getColor(android.R.color.white));
tollAmount.setTextColor(getResources().getColor(android.R.color.black));
}
});

该函数检查文本框上的值是否为空或 null 以外的任何值。因此,如果用户在文本框中输入内容,背景和前景色应该改变。但这并没有发生。知道如何编辑它吗?

最佳答案

我认为您正在寻找的是 TextWatcher(尽管 onFocusChanged 可能有效,但这是另一种方法)。这是示例代码:

TextWatcher watcher= new TextWatcher() {
public void afterTextChanged(Editable s) {
if (TextBox1.getText().toString().equals("")) {
TextBox1.setBackgroundColor(getResources().getColor(android.R.color.white));
TextBox1.setTextColor(getResources().getColor(android.R.color.black));
}

}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//Do something or nothing.
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
//Do something or nothing
}
};

TextBox1.addTextChangedListener(watcher);

您还需要使用 .equals() 进行字符串比较。

关于java - Android EditText onChangeListener 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17836025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com