它是如何可能的,如果一个字符被输入到一个EditText这使得它从0到1个字符,然后执行一些动作来检测?
Answer 1:
既然你有一个相当抽象的问题,让我尝试同样通用的答案:
在您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
Answer 2:
您可以使用TextWatcher您的EditText和处理任何你想要立即与你的用户输入数据的事情。
这里是格式化时立即用户输入数据的示例
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
}
});
Answer 3:
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
文章来源: Detect when user enters data into edittext immediately shows answer [closed]