简体   繁体   English

Android在活动上添加onClick在软键盘上

[英]Android adding onClick on soft keyboard on activity

I am trying to add an onClick on the soft keyboard for an activity. 我正在尝试在活动的软键盘上添加onClick。 The reason why is that i want to check if the user is currently active. 原因是我要检查用户当前是否处于活动状态。 So what i have done is that if the user clicks on the app i will reset a inactivity timer. 所以我所做的是,如果用户单击该应用程序,我将重置一个不活动计时器。 The problem is that when a user interacts with the soft keyboard it doesn't call the function onUserInteraction() which is a function I override in the activity. 问题在于,当用户与软键盘进行交互时,它不会调用函数onUserInteraction() ,该函数是我在活动中覆盖的函数。 So i need help to find a way to keep track if the soft keyboard has been clicked for every textfield etc I have in the activity. 因此,我需要帮助以找到一种方法来跟踪我是否在活动中针对每个文本字段等单击了软键盘。 (I know that i can insert a onclick listerner on every EditText field but i rather not do that, because if I would use many EditText fields it would not be so nice) (我知道我可以在每个EditText字段上插入一个onclick listerner,但我宁愿不这样做,因为如果我使用许多EditText字段,那就不太好了)

To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. 要处理单个按键,请适当地实现onKeyDown()或onKeyUp()。 Usually, you should use onKeyUp() if you want to be sure that you receive only one event. 通常,如果要确保仅收到一个事件,则应使用onKeyUp()。 If the user presses and holds the button, then onKeyDown() is called multiple times. 如果用户按住按钮,则会多次调用onKeyDown()。

Create a class some thing like UserInteractionEditText and extend EditText and set the onclick lisetener in that class use that class in all the XML layouts as you use EditText you can do some thing like this: 创建一个类似于UserInteractionEditText类的类,并扩展EditText并设置该类中的onclick lisetener,在您使用EditText在所有XML布局中使用该类,您可以执行以下操作:

    public class UserInteractionEditText extends EditText implements View.OnClickListener {


    public UserInteractionEditText(Context context) {
        super(context);
    }

    public UserInteractionEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public UserInteractionEditText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    @Override
    public void onClick(View v) {
        //TODO:: Handle user Click Events
    }
}

So this is what i ended up with. 这就是我最终得到的。 I was hoping for something else, but this solves the problem. 我希望有其他东西,但这可以解决问题。 Thanks for the help! 谢谢您的帮助!

public class ActivityEditText extends android.support.v7.widget.AppCompatEditText {
        private TextWatcher tw;
        public ActivityEditText(Context c)
        {
            super(c);
            this.setOurTCL();
        }

        public ActivityEditText(Context context, AttributeSet attrs) {
            super(context, attrs);
            this.setOurTCL();
        }

        public ActivityEditText(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            this.setOurTCL();
        }

        private void setOurTCL()
        {
            this.tw = new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    InactivityManager.resetTime();
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            };
            this.addTextChangedListener(this.tw);
        }

        @Override
        public void removeTextChangedListener(TextWatcher watcher) {
            if(!watcher.equals(this.tw))
                super.removeTextChangedListener(watcher);
        }
    }

You can use onUserInteraction() function of activity. 您可以使用活动的onUserInteraction()函数。 You need to override this function in your activity. 您需要在活动中覆盖此功能。 This function get calls when you perform any kind of interaction with your activity. 当您与活动进行任何形式的交互时,此函数都会调用。

 @Override
    public void onUserInteraction() {
        super.onUserInteraction();
        // Your code goes here
    }

You can refer this example and also this answer , refer the docs here 您可以参考此示例以及此答案 ,请参考此处的文档

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM