onUserInteraction未被调用

3
我希望实现用户不活动事件。我有许多屏幕,每个屏幕都有很多编辑框。因此,我实现了onUserInteraction()方法来检测屏幕上的用户操作。但是,不幸的是,当用户在光标位于编辑框内时输入时,onUserInteraction()永远不会被调用。一种可能的解决方案是实现文本更改监听器,但我不想在所有编辑框中实现它。
那么,是否有其他解决方案,以便我可以在活动本身内获得按键按下事件?
2个回答

1
你可以为活动中的所有编辑文本创建一个通用的 TextWatcher。
声明:
private class GenericTextWatcher implements TextWatcher{

    private View view;
    private GenericTextWatcher(View view) {
        this.view = view;
    }

    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

    public void afterTextChanged(Editable editable) {
        String text = editable.toString();
        switch(view.getId()){
            case R.id.name:
                model.setName(text);
                break;
            case R.id.email:
                model.setEmail(text);
                break;
            case R.id.phone:
                model.setPhone(text);
                break;
        }
    }
}

使用方法:

name = (EditText) findViewById(R.id.name);
name.setText(model.getName());
name.addTextChangedListener(new GenericTextWatcher(name));

email = (EditText) findViewById(R.id.email);
email.setText(model.getEmail());
email.addTextChangedListener(new GenericTextWatcher(email));

phone = (EditText) findViewById(R.id.phone);
phone.setText(model.getPhone());
phone.addTextChangedListener(new GenericTextWatcher(phone));

谢谢您的回复,但我正在寻找一般解决方案,因为有相当多的编辑文本框。 - Smeet

-1

我假设您已经为所有活动创建了基本活动。如果是这种情况,您只需要在基本活动中实现TextWatcher。并且在每个EditText中使用setAddTextChangeListener(this)。每当任何活动的任何EdiText中的文本更改时,您都会收到回调到基本活动。希望这可以帮助您。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接