[英]Android Development: How To Use onKeyUp?
I'm new to Android development and I can't seem to find a good guide on how to use an onKeyUp
listener.我是 Android 开发的新手,我似乎找不到关于如何使用
onKeyUp
侦听器的好指南。
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
.在我的应用程序中,我有一个很大的
EditText
,当有人按下并释放该EditText
一个键时,我想调用一个函数,该函数将在该EditText
执行正则表达式。
I don't know how I'd use the onKeyUp.我不知道如何使用 onKeyUp。 Could someone please show me how?
有人可以告诉我怎么做吗?
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);
CORRECTION:更正:
For a while, I used a generic onKeyListener .有一段时间,我使用了一个通用的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. "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.我发现 Activity 中的每个控件都会自动调用
onKeyUp()
。 If this is what you want, add it to the Activity just like you add the onCreate()
如果这是你想要的,就像你添加
onCreate()
一样将它添加到 Activity
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.我知道这是一个老问题,但也许这会帮助其他人解决同样的问题。
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.这里最后我们调用了 super.onkeyUp 方法。 Which handles the event when the user do not presses the valid key.
当用户没有按下有效键时处理事件。
For more details you may consider following link .有关更多详细信息,您可以考虑以下链接。
Just for an EditText
.仅用于
EditText
。 its achieve by setOnClickListener()
itself.它通过
setOnClickListener()
本身实现。 No Need for the onKeyUp()
.不需要
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
}
}
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);
}
});
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;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
actionLabel.setText("KEYCODE_BACK key pressed");
Toast.makeText(this, "Press again back for exit", Toast.LENGTH_SHORT).show();
counter++;
if (counter > 1) {
super.onBackPressed();
}
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
actionLabel.setText("VOLUME_UP key pressed");
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
actionLabel.setText("VOLUME_DOWN key pressed");
return true;
}
return super.onKeyDown(keyCode, event);
}
// catches the onKeyUp button event
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
actionLabel.setText("KEYCODE_BACK key released");
return true;
case KeyEvent.KEYCODE_VOLUME_UP:
actionLabel.setText("VOLUME_UP key released");
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
actionLabel.setText("VOLUME_DOWN key released");
return true;
}
return super.onKeyDown(keyCode, event);
}
// works for API level 5 and lower
@Override
public void onBackPressed() {
actionLabel.setText("BACK key pressed");
super.onBackPressed();
}
// catches the long press button event (longer than 2 seconds)
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
Toast.makeText(this, "Pressed for a long time", Toast.LENGTH_SHORT).show();
return true;
}
// catches the on touch event on screen and shows the specific pixels
// touched
@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();
actionLabel.setText("Touch press on x: " + x + " y: " + y);
return true;
}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.