Android:编辑文本转到按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3276448/
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
Android: Edit Text Go Button
提问by Corey Alexander
I have an Edit Text that is defined as follows.
我有一个定义如下的编辑文本。
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text"
android:hint="@string/field_text"
android:id="@+id/field"
/>
I want to set a custom command so that when somebody clicks on the Done/Go button on the onscreen keyboard a button is clicked or just run the methods that are run by the button. I think this has something to do with ime options but I havent been able to figure out how they work. Thanks in advance for any help!
我想设置一个自定义命令,以便当有人单击屏幕键盘上的“完成/开始”按钮时,会单击一个按钮或仅运行该按钮运行的方法。我认为这与 ime 选项有关,但我无法弄清楚它们是如何工作的。在此先感谢您的帮助!
回答by Niall Sheridan
You want a combination of android:imeOptions and setOnEditorActionListener
你想要 android:imeOptions 和 setOnEditorActionListener 的组合
<EditText android:id="@+id/some_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:imeOptions="actionSend">
</EditText>
some_edittext.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
some_button.performClick();
return true;
}
return false;
}
});
Obviously you should change actionSend to the action you want, and update IME_ACTION_SEND correspondingly.
显然,您应该将 actionSend 更改为您想要的操作,并相应地更新 IME_ACTION_SEND。
回答by adamp
Take a look at the setImeActionLabel
method (or imeActionLabel
and imeActionId
attributes) and setOnEditorActionListener
to set a listener to respond to the events.
查看setImeActionLabel
方法(或imeActionLabel
和imeActionId
属性)并setOnEditorActionListener
设置侦听器以响应事件。