[英]EditText + Button onClick when the user press enter
I have the following structure in my main layout: 我的主要布局具有以下结构:
LinearLayout
EditText
EditText
Checkbox
Button
I'd like that "enter" key at the second EditText
to cast a onClick
event at the button. 我想要第二个EditText
上的“ enter”键在按钮上投射一个onClick
事件。 How can I do it? 我该怎么做? Is possible to do it with only changing the xml? 仅更改xml就可以做到吗?
Thanks in advance 提前致谢
You can use this 你可以用这个
((EditText)findViewById(R.id.edittext)).setOnEditorActionListener(
new EditText.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
mButton.performClick();
return true;
}
return false;
}
});
You can set the behavior for clicking the button in XML. 您可以设置单击XML中按钮的行为。 Make a method with the behavior you want to have happen when the button is clicked. 使用单击按钮时要发生的行为制作一种方法。 Then, call it for the button by using android:onClick 然后,使用android:onClick将其称为按钮
For the EditText, I believe you can't do it in XML and have to do it as in the answer from user7777777777. 对于EditText,我相信您不能使用XML来做到这一点,而必须按照user7777777777的回答来做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.