本文介绍了的EditText - 改变文本的同时输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要更换的EditText内的文本,同时输入:例如:如果用户pressedA将它保存到缓冲区,并在EditText上D显示,而不是(貌似他pressedD)。现在,我可以读pressed字符,但我不能在等显示任何字符,避免计算器:
最后的EditText等=(EditText上)findViewById(R.id.editTexts);
最后的TextView电视=(TextView中)findViewById(R.id.textView2);
et.addTextChangedListener(新TextWatcher()
{
公共无效afterTextChanged(编辑S){}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
}
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
如果(s.length()大于0){
tv.setText(s.toString()子(s.length() - 1));
et.setText();
}
}
});
解决方案
您可以将其作为改变要求::
公共类SampleActivity延伸活动{
TextWatcher TT = NULL;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);
最后的EditText等=(EditText上)findViewById(R.id.editText1);
最后的TextView电视=(TextView中)findViewById(R.id.textView1);
TT =新TextWatcher(){
公共无效afterTextChanged(编辑S){
et.setSelection(s.length());
}
公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){}
公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
et.removeTextChangedListener(TT);
et.setText(et.getText()的toString()代替(A,C));
et.addTextChangedListener(TT);
}
};
et.addTextChangedListener(TT);
}
}
I need to replace the text inside the EditText while typing :Example : if the user pressed "A" it would be stored into a buffer and on the EditText "D" is displayed instead (looks like he pressed "D").Now I can read the pressed character but I can't display any character in the et to avoid stackoverflow :
final EditText et = (EditText) findViewById(R.id.editTexts);
final TextView tv = (TextView) findViewById(R.id.textView2);
et.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) {
if(s.length() > 0) {
tv.setText(s.toString().substring(s.length()-1));
et.setText("");
}
}
});
解决方案
You can change it as required::
public class SampleActivity extends Activity {
TextWatcher tt = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText et = (EditText) findViewById(R.id.editText1);
final TextView tv = (TextView) findViewById(R.id.textView1);
tt = new TextWatcher() {
public void afterTextChanged(Editable s){
et.setSelection(s.length());
}
public void beforeTextChanged(CharSequence s,int start,int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count) {
et.removeTextChangedListener(tt);
et.setText(et.getText().toString().replace("A", "C"));
et.addTextChangedListener(tt);
}
};
et.addTextChangedListener(tt);
}
}
这篇关于的EditText - 改变文本的同时输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!