- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下代码:
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/
是否可以仅在软键盘输入数字时为 numberPicker 设置 onChangeListener?当数字更改为 gy +/- 时,不应执行监听器。仅当用户从软键盘输入数字时。 目标是在用户在前一个项目
我写了一个简单的PreferenceScreen,如下所示: public class SettingsActivity extends PreferenceActivity implements O
我正在尝试重置 ComboBox 的选择,如下所示: // private ListView f_lItems f_lItems.getSelectionModel().selectedItemPro
我用 XML 创建了一个 RadioGroup 布局。所以我动态创建它: RadioGroup segmentRadioGroup = new RadioGroup(parentActivity);
我有以下代码: nameOfInf.setOnFocusChangeListener(new OnFocusChangeListener() { if (strTollAmount.lengt
我有这段代码来为 Range Bar 库 实现监听器: rangebar.setOnRangeBarChangeListener(new RangeBar.OnRangeBarChangeListen
我是一名优秀的程序员,十分优秀!