问题描述
我有一个EditText。在这种的EditText用户输入一些文字。当用户点击上的空格键,我需要执行一些动作。我怎么能做到这一点。
I have an edittext. In that edittext user enters some text. when user hits on space key, i need to perform some action. how can i do it.
在此先感谢。
推荐答案
要做些什么,当用户presses您使用下面的空格键 -
To do something when the user presses the space key you use the following -
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(key == KeyEvent.KEYCODE_SPACE)){
//put your code for whatever you what to do when the user presses space here
}
}
Problems在EditText上
如果您有多个edittexts,在上述code将
If you have multiple edittexts, in the above code put
if (edittext.hasFocus()){
//do something for particular edit text
}
我想我知道为什么你的EditText不工作。当EditText上聚焦(选择),按键点击次数绕过你的活动,并去直奔的EditText。为了解决这个问题,你需要移动 onKeyListener
到 onKeyListener
为您的EditText。
I think I know why your edittext is not working. When the edittext is focused(selected), the key clicks are bypassing your activity and go in straight to the edittext. To solve this you need to move the onKeyListener
into an onKeyListener
for your edittext.
试试下面的code,而不是
Try the below code instead
edittext.setOnKeyListener(new OnKeyListener() {
@Override
public boolean onKey(View v, int key, KeyEvent event) {
if (event.getAction()==KeyEvent.ACTION_DOWN && key == KeyEvent.KEYCODE_ENTER) {
//operation that you want on key press
return true;
}
return false;
}
});
希望这一个帮助
Hope this one helps
这篇关于如何显示在softkeyboard空格键和执行一些动作,当用户点击空格键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!