java EditText onChangeListener 函数 Android
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17836025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
EditText onChangeListener function Android
提问by Si8
I have the following code:
我有以下代码:
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));
}
});
The function checks to see if the value on the textbox is anything other than empty or null. So if the user enters something in the text box the background and foreground color should change. But that's not happening. Any idea how to edit it?
该函数检查文本框上的值是否为空或空以外的任何值。因此,如果用户在文本框中输入内容,背景和前景色应该会改变。但事实并非如此。知道如何编辑它吗?
回答by TronicZomB
I think what you are looking for is the TextWatcher (though the onFocusChanged might work, but this is another approach). Here is sample code:
我认为您正在寻找的是 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);
Also you will want to do String comparisons using .equals()
.
您还需要使用.equals()
.
回答by user1140237
txt = (EditText) findViewById(R.id.txtChange);
txt.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
if (TextUtils.isEmpty(s.toString().trim())) {
txt.setBackgroundColor(Color.RED);
} else
txt.setBackgroundColor(Color.GREEN);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
}
});
回答by Sadegh
You can your TextWatcher
, here is an example:
你可以你的TextWatcher
,这是一个例子:
final EditText et = new EditText(getApplicationContext());
TextWatcher tw = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() > 0) {
et.setBackgroundColor(Color.WHITE);
} else {
et.setBackgroundColor(Color.GRAY);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
};
et.addTextChangedListener(tw);
setContentView(et);
回答by Simon Dorociak
Your approach is i think absolutely wrong (it compiles?). You need to test content length of field. I don't know exactly what you are trying to do in your code.
你的方法是我认为绝对错误的(它编译?)。您需要测试字段的内容长度。我不知道你到底想在你的代码中做什么。
Here are two possible approaches:
这里有两种可能的方法:
if (TextBox1.getText().toString().length() > 0) {
// is not empty
}
else {
// is empty
}
or
if (!TextUtils.isEmpty(TextBox1.getText().toString())) {
// is not empty
}
else {
// is empty
}