Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何抓住一个;“完成”;从软键盘上按键_Android - Fatal编程技术网

Android 如何抓住一个;“完成”;从软键盘上按键

Android 如何抓住一个;“完成”;从软键盘上按键,android,Android,如何从软键盘捕获特定的按键事件? 我对“完成”键特别感兴趣。注意:这个答案很旧,不再有效。请参见下面的答案 捕获KeyEvent,然后检查其keycode。FLAG_EDITOR_操作用于识别来自输入法的输入键,该输入法的输入键已自动标记为“下一步”或“完成” 找到文档 我不太确定在被接受的答案中使用了哪种听众。 我使用了连接到EditText的OnKeyListener,它无法捕获下一个或完成 但是,使用OnEditorActionListener是有效的,它还允许我通过将操作值与定义的常量E

如何从软键盘捕获特定的按键事件?
我对“完成”键特别感兴趣。

注意:这个答案很旧,不再有效。请参见下面的答案

捕获KeyEvent,然后检查其keycode。FLAG_EDITOR_操作用于识别来自输入法的输入键,该输入法的输入键已自动标记为“下一步”或“完成”


找到文档

我不太确定在被接受的答案中使用了哪种听众。 我使用了连接到
EditText
OnKeyListener
,它无法捕获下一个或完成

但是,使用
OnEditorActionListener
是有效的,它还允许我通过将操作值与定义的常量
EditorInfo.IME\u action\u NEXT
EditorInfo.IME\u action\u DONE
进行比较来区分它们

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ((actionId & EditorInfo.IME_MASK_ACTION) != 0) {
            doSomething();
            return true;
        }
        else {
            return false;
        }
    }
});

@Swato的回答对我来说并不完整(并且没有编译!),因此我将展示如何与已完成和下一个操作进行比较

editText.setOnEditorActionListener(new OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
        int result = actionId & EditorInfo.IME_MASK_ACTION;
        switch(result) {
        case EditorInfo.IME_ACTION_DONE:
            // done stuff
            break;
        case EditorInfo.IME_ACTION_NEXT:
            // next stuff
            break;
        }
    }
});
我还想指出,对于JellyBean和更高版本的OnEditorActionListener,需要监听'enter'或'next',而不能使用OnKeyListener。从文档中:

由于软输入方法可以使用多种创造性的文本输入方式,因此不能保证软键盘上的任何按键都会生成按键事件:这由IME自行决定,实际上不鼓励发送此类事件。在软输入法中,任何键都不应依赖于接收KeyEvents


参考资料:

我有一个搜索姓名的EditText,它会在ListView中自动显示下面的结果。软输入键盘只显示了“下一步”按钮和输入符号-这并没有做任何事情。我只想要“完成”按钮(并没有下一步或输入标志),而且我还想要它在按下时,它应该关闭键盘,因为用户应该看到它下面的结果

我在Cyril Mottier先生的博客上找到的解决方案非常简单,而且没有任何额外的代码: 在EditText所在的xml中,应写入: android:imeOptions=“actionDone”

因此,隐藏带有“完成”按钮的键盘,EditText应如下所示:

<EditText
        android:id="@+id/editText1central"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/imageView1"
        android:layout_toLeftOf="@+id/imageView2tie"
        android:ems="10"
        android:imeOptions="actionDone"
        android:hint="@string/trazi"
        android:inputType="textPersonName" />

从软键盘覆盖活动的onKeyUp方法捕获“完成”按键。 为视图设置OnKeyListener侦听器将不起作用,因为软件输入方法中的按键通常不会触发此侦听器的方法,在视图中按下硬件键时会调用此回调

// Called when a key was released and not handled by any of the views inside of the activity. 
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
        case KeyEvent.KEYCODE_ENTER:
            // code here
            break;
        default:
            return super.onKeyUp(keyCode, event);
    }
    return true;
}

注意:在编辑文本中输入提及

<EditText android:id="@+id/select_category" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" >

edittext.setOnEditorActionListener(new OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ((actionId & EditorInfo.IME_MASK_ACTION) == EditorInfo.IME_ACTION_DONE) {
                    //do something here.
                    return true;
                }
                return false;
            }
        });

setOnEditorActionListener(新的OnEditorActionListener(){
@凌驾
公共布尔onEditorAction(TextView v、int actionId、KeyEvent事件){
if((actionId&EditorInfo.IME\u MASK\u ACTION)==EditorInfo.IME\u ACTION\u DONE){
//在这里做点什么。
返回true;
}
返回false;
}
});
只需这样做:

editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if(actionId == EditorInfo.IME_ACTION_DONE)
    {
       //Do Something
    }

    return false;
}
});

IME_MASK_ACTION为255,而收到的actionId为6,并且我的编译器不接受

if (actionId & EditorInfo.IME_MASK_ACTION) 
这是一个整数。&-ing 255还有什么用?因此,测试可以简单地进行

public boolean onEditorAction(TextView view, int actionId, KeyEvent event) {
    if (actionId == EditorInfo.IME_ACTION_DONE)
    ...

您可以通过以下方法覆盖“完成”键事件:

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_DONE) {
            // do your stuff here
        }
        return false;
    }
});
KOTLIN版本:

<EditText android:id="@+id/edit_text" 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="text" />

不要忘记设置android:inputType。

// Get reference to EditText.
val editText = findViewById<EditText>(R.id.edit_text)

editText.setOnEditorActionListener { _, actionId: Int, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Do your logic here.
        true
    } else {
        false
    }
}
//获取对EditText的引用。
val editText=findviewbyd(R.id.edit_text)
editText.setOnEditorActionListener{{,actionId:Int,{->
if(actionId==EditorInfo.IME\u ACTION\u DONE){
//在这里做你的逻辑。
真的
}否则{
假的
}
}

如何找到软键盘上每个按键的按键代码???@BhavanaVadodariya您无法找到。至少不是在果冻豆上。请参阅:对于JellyBean和更高版本,此用例需要使用OnEditorActionListener而不是OnKeyListener。从文档中可以看出:由于软输入方法可以使用多种创造性的文本输入方式,因此不能保证软键盘上的任何按键都会生成按键事件:这由IME自行决定,事实上,不鼓励发送此类事件。在软输入法中,任何键都不应依赖于接收KeyEvents。参考资料:@JasonAxelson的答案是最好的这一行>>如果(actionId&EditorInfo.IME\u MASK\u ACTION)都
OnEditorActionListener
OnKeyListener
对我有效。不幸的是,这个答案缺乏上下文:(不再是有效答案。请看投票最多的答案。在示例中,可能还值得返回true或false,这正是我所要查找的;-)即使在每个
编辑文本中都很有效!谢谢:-)这绝对是正确的答案!非常感谢。
<EditText android:id="@+id/edit_text" 
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:inputType="text" />
// Get reference to EditText.
val editText = findViewById<EditText>(R.id.edit_text)

editText.setOnEditorActionListener { _, actionId: Int, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        // Do your logic here.
        true
    } else {
        false
    }
}