如何检测一个字符是否输入到 EditText 中,使其长度为 0 到 1 个字符,然后执行一些操作?
问问题
25350 次
3 回答
25
既然你有一个相当抽象的问题,让我尝试一个同样通用的答案:
在你的onCreate()
, 声明并转换你的EditText
EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(filterTextWatcher);
然后,在外部,onCreate()
设置filterTextWatcher
如下:
private TextWatcher filterTextWatcher = new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// DO THE CALCULATIONS HERE AND SHOW THE RESULT AS PER YOUR CALCULATIONS
int radius = 0;
radius = Integer.valueof(s.toString);
double area = Math.PI * radius * radius;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};
编辑:
更新了可能计算的代码。(未经测试的代码:我只是在没有测试的情况下输入了它。在必要时修改)
在此处阅读有关TextWatcher 的更多信息
这里有一些示例可以帮助您入门:
- http://www.android-ever.com/2012/06/android-edittext-textwatcher-example.html
- http://www.cybernetikz.com/blog/android-textwatcher-example/
- http://www.allappsdevelopers.com/TopicDetail.aspx?TopicID=22b00052-dad0-4e09-a07e-b74f115ab247
- http://myandroidsolutions.blogspot.in/2012/06/android-edittext-change-listener.html
于 2013-01-31T12:13:57.627 回答
7
您可以在 EditText 上使用TextWatcher并立即处理您想要对用户输入数据执行的任何操作。
这是用户输入数据时立即格式化的示例
final EditText EditTxtFinancialCode = (EditText) findViewById(R.id.edtNewCpFinancialCode);
EditTxtFinancialCode.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
String a = "";
boolean flag = true;
String eachBlock[] = EditTxtFinancialCode.getText().toString().split("-");
for (int i = 0; i < eachBlock.length; i++) {
if (eachBlock[i].length() > 4) {
flag = false;
}
}
if (flag) {
EditTxtFinancialCode.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_DEL)
KeyDel = 1;
return false;
}
});
if (KeyDel == 0) {
if (((EditTxtFinancialCode.getText().length() + 1) % 5) == 0) {
if (EditTxtFinancialCode.getText().toString().split("-").length <= 2) {
EditTxtFinancialCode.setText(EditTxtFinancialCode.getText() + "-");
EditTxtFinancialCode.setSelection(EditTxtFinancialCode.getText().length());
}
}
a = EditTxtFinancialCode.getText().toString();
} else {
a = EditTxtFinancialCode.getText().toString();
KeyDel = 0;
}
} else {
EditTxtFinancialCode.setText(a);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
于 2013-01-31T12:09:00.710 回答
6
http://developer.android.com/reference/android/widget/TextView.OnEditorActionListener.html
做一个听众。
myTextBox.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
TextView myOutputBox = (TextView) findViewById(R.id.myOutputBox);
myOutputBox.setText(s);
}
});
见http://www.mysamplecode.com/2012/06/android-edittext-text-change-listener.html
于 2013-01-31T12:14:37.640 回答