在同一个 Activity 上实现多个 TextWatcher
我在 Activity 中实现 TextWatcher:
public class Coordinate extends Activity implements TextWatcher {
/** Called when the activity is first created. */
......
然后
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
........
我的部分问题是拥有多个 TextChangedListener 会导致应用程序发生 FC
txtDdLatDeg.addTextChangedListener(this);
txtDMmLatDeg.addTextChangedListener(this);
txtDMSLatDeg.addTextChangedListener(this);
然后
@Override
public void afterTextChanged(Editable s) {
String c = s.toString(); // read Content
// stuff to do later
((EditText)findViewById(R.id.txtDMSLatDeg)).setText(c);
((EditText)findViewById(R.id.txtDdLatDeg)).setText(c);
return;
} // End of TextChanged method
我需要能够更新一个 EditText 并让另外两个即时更新。
我似乎只能在只有一个 EditText 具有 addChangeListener
时才能使其工作。
我似乎也无法为各个 EditText 字段实现单独的 afterTextChanged
方法。
I implement TextWatcher in the Activity:
public class Coordinate extends Activity implements TextWatcher {
/** Called when the activity is first created. */
......
Then
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
........
Part of my problem is that having more than one TextChangedListener causes the app to FC
txtDdLatDeg.addTextChangedListener(this);
txtDMmLatDeg.addTextChangedListener(this);
txtDMSLatDeg.addTextChangedListener(this);
Then
@Override
public void afterTextChanged(Editable s) {
String c = s.toString(); // read Content
// stuff to do later
((EditText)findViewById(R.id.txtDMSLatDeg)).setText(c);
((EditText)findViewById(R.id.txtDdLatDeg)).setText(c);
return;
} // End of TextChanged method
I need to be able to update one EditText and have the other two update on the fly.
I can only seem to make it works when only one EditText has the addChangeListener
.
I also cannot seem to implement a seperate afterTextChanged
method for the individual EditText fields.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后将它们创建为实例变量:
然后你可以这样做:
Then create them as instance variables:
Then you can do:
好的,我通过在
afterTextChanged
方法之前在 EditText 上使用onFocus()
解决了这个问题:我为每个 EditText 创建一个实例变量 我需要观察/操作的盒子。
Ok, I solved this by using
onFocus()
on the EditText before theafterTextChanged
method:I create an instance variable for each EditText box I need to watch/manipulate.