Android Development: How To Use onKeyUp?

2020-02-10 12:42发布

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

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

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

6条回答
Fickle 薄情
2楼-- · 2020-02-10 12:44

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:

        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;
        }
查看更多
贼婆χ
3楼-- · 2020-02-10 12:46

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_object.setOnClickListener(new OnClickListener() {
     public void onClick(View v) {
    //do what you need 
    }
}
查看更多
爱情/是我丢掉的垃圾
4楼-- · 2020-02-10 12:52

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);
    }
});
查看更多
疯言疯语
5楼-- · 2020-02-10 12:58

The very right way is to use TextWatcher class.

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);
查看更多
男人必须洒脱
6楼-- · 2020-02-10 13:06

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.

For more details you may consider following link.

查看更多
叛逆
7楼-- · 2020-02-10 13:07

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 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()

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.

查看更多
登录 后发表回答