在同一个 Activity 上实现多个 TextWatcher

发布于 2024-10-11 05:19:17 字数 1105 浏览 2 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酷遇一生 2024-10-18 05:19:17

然后将它们创建为实例变量:

TextWatcher watcher1 = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    @Override
    public void afterTextChanged(Editable s) { }
};
TextWatcher watcher2 = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence c, int i, int i1, int i2) {}
    @Override
    public void onTextChanged(CharSequence c, int i, int i1, int i2) {}
    @Override
    public void afterTextChanged(Editable s) { }
};

然后你可以这样做:

txtDdLatDeg.addTextChangedListener(watcher1);
txtDMmLatDeg.addTextChangedListener(watcher1);

Then create them as instance variables:

TextWatcher watcher1 = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    @Override
    public void afterTextChanged(Editable s) { }
};
TextWatcher watcher2 = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence c, int i, int i1, int i2) {}
    @Override
    public void onTextChanged(CharSequence c, int i, int i1, int i2) {}
    @Override
    public void afterTextChanged(Editable s) { }
};

Then you can do:

txtDdLatDeg.addTextChangedListener(watcher1);
txtDMmLatDeg.addTextChangedListener(watcher1);
七月上 2024-10-18 05:19:17

好的,我通过在 afterTextChanged 方法之前在 EditText 上使用 onFocus() 解决了这个问题:

onCreate (Bundle icicle) {
    // Usual stuff here
    txtDdLatDeg.addTextChangeListener(watcher1);
}

TextWatcher watcher1 = new TextWatcher() {
    if (txtDdLatDeg.hasFocus()) {
        @Override
        public void afterTextChanged(Editable s) {
            String c = s.toString();
            ((EditText)findViewById(R.id.txtDMSLatDeg)).setText(c); 
            ((EditText)findViewById(R.id.txtDMmLatDeg)).setText(c);     
        }
}};

我为每个 EditText 创建一个实例变量 我需要观察/操作的盒子。

Ok, I solved this by using onFocus() on the EditText before the afterTextChanged method:

onCreate (Bundle icicle) {
    // Usual stuff here
    txtDdLatDeg.addTextChangeListener(watcher1);
}

TextWatcher watcher1 = new TextWatcher() {
    if (txtDdLatDeg.hasFocus()) {
        @Override
        public void afterTextChanged(Editable s) {
            String c = s.toString();
            ((EditText)findViewById(R.id.txtDMSLatDeg)).setText(c); 
            ((EditText)findViewById(R.id.txtDMmLatDeg)).setText(c);     
        }
}};

I create an instance variable for each EditText box I need to watch/manipulate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文