Java Android 开发:如何使用 onKeyUp?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3741147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 04:11:03  来源:igfitidea点击:

Android Development: How To Use onKeyUp?

javaandroidonkeyup

提问by AlexPriceAP

I'm new to Android development and I can't seem to find a good guide on how to use an onKeyUplistener.

我是 Android 开发的新手,我似乎找不到关于如何使用onKeyUp侦听器的好指南。

In my app, I have a big EditText, when someone presses and releases a key in that EditTextI want to call a function that will perform regular expressions in that EditText.

在我的应用程序中,我有一个很大的EditText,当有人按下并释放一个键时,EditText我想调用一个将在该EditText.

I don't know how I'd use the onKeyUp. Could someone please show me how?

我不知道如何使用 onKeyUp。有人可以告诉我怎么做吗?

采纳答案by Damjan

The very right way is to use TextWatcher class.

非常正确的方法是使用 TextWatcher 类。

EditText tv_filter = (EditText) findViewById(R.id.filter);

TextWatcher fieldValidatorTextWatcher = new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (filterLongEnough()) {
                populateList();
            }
        }

        private boolean filterLongEnough() {
            return tv_filter.getText().toString().trim().length() > 2;
        }
    };
    tv_filter.addTextChangedListener(fieldValidatorTextWatcher);

回答by Praveen

Just for an EditText. its achieve by setOnClickListener()itself. No Need for the onKeyUp(). Because that will perform by a click/Touch Event. right? Just do this.

只是为了一个EditText. 它自己实现setOnClickListener()。不需要onKeyUp(). 因为这将通过单击/触摸事件执行。对?就这样做。

edittext_object.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
    //do what you need 
    }
}

回答by Milan

Writing this out of my head but it should be among the following lines:

把这个写在我的脑海里,但它应该在以下几行中:

text.setKeyListener(new KeyListener() {
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {


        if(keyCode == KeyEvent.KEYCODE_0) dosomething // pick the custom key from keyEvent


        return super.onKeyUp(keyCode, event);
    }
});

回答by R Hughes

CORRECTION:

更正:

For a while, I used a generic onKeyListener. I soon found that my code was being called twice. Once with the key down and once with key up. I now use the following listener and only call the code once. "if (event.getAction() == KeyEvent.ACTION_UP)"is the key.

有一段时间,我使用了一个通用的onKeyListener。我很快发现我的代码被调用了两次。一次按下键,一次按下键。我现在使用以下侦听器并且只调用一次代码。 "if (event.getAction() == KeyEvent.ACTION_UP)"是关键。

OnKeyListener keyListener = new OnKeyListener() {

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_UP) {
            //do something here
        }
        return false;
    }
};

I found that onKeyUp()is called automatically for every control in the Activity. If this is what you want, add it to the Activity just like you add the onCreate()

我发现onKeyUp()Activity 中的每个控件都会自动调用它。如果这是您想要的,请将其添加到活动中,就像添加onCreate()

Example:

例子:

public boolean onKeyUp(int keyCode, KeyEvent event) {
    //do something here
    return false;
};

I know this is a old question, but maybe this will help others with the same issue.

我知道这是一个老问题,但也许这会帮助其他人解决同样的问题。

回答by cammando

You may consider using the following code:

您可以考虑使用以下代码:

 @Override 
 public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        ...
        case KeyEvent.KEYCODE_J:
            if (event.isShiftPressed()) {
                fireLaser();
            } else {
                fireMachineGun();
            }
            return true;
        case KeyEvent.KEYCODE_K:
            if (event.isShiftPressed()) {
                fireSeekingMissle();
            } else {
                fireMissile();
            }
            return true;
        default:
            return super.onKeyUp(keyCode, event);
    } }

Here In the end we have called the super.onkeyUp method. Which handles the event when the user do not presses the valid key.

这里最后我们调用了 super.onkeyUp 方法。当用户没有按下有效键时处理事件。

For more details you may consider following link.

有关更多详细信息,您可以考虑以下链接

回答by Udi Reshef

If you use device with no touch but with hard keypad buttons (keyboard) use this code to control the events of moving left right up down and ok. use onkeyDown and not onKeyUp because the onKeyup will return the next button events:

如果您使用没有触摸但带有硬键盘按钮(键盘)的设备,请使用此代码来控制向左向右向下移动的事件。使用 onkeyDown 而不是 onKeyUp 因为 onKeyup 将返回下一个按钮事件:

        Button myButton = (Button) findViewById(R.id.my_btn);
        myButton .setKeyListener(new KeyListener() {
        @Override
        public int getInputType() {
            return 0;
        }

        @Override
        public boolean onKeyDown(View view, Editable editable, int keyCode, KeyEvent keyEvent) {
            Log.d("myTag", "onKeyDown code: " +keyCode);
            switch (keyCode) {
                case KeyEvent.KEYCODE_DPAD_RIGHT:
                    // USER_MOVE_RIGHT();
                    return true;
                case KeyEvent.KEYCODE_DPAD_LEFT:
                    //USER_MOVE_LEFT());
                    return true;
                case KeyEvent.KEYCODE_DPAD_DOWN:
                    //USER_MOVE_DOWN());
                    return true;
                case KeyEvent.KEYCODE_DPAD_UP:
                    //USER_MOVE_UP();
                    return true;
                case KeyEvent.KEYCODE_DPAD_CENTER:
                    //USER_Press_OK()
                    return true;
                               }
            return false;
        }