有没有办法让Enter键提交我的表格?现在我有一个使用addListenerButton()
发送的登录按钮...我可以以某种方式将Enter键链接到那个吗?我认为我的.java中的代码是无关紧要的,但如果请求,我总是可以发布它。
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:id="@+id/back"
android:background="@drawable/a_hdpi">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:padding="10dp"
android:background="@drawable/linearlayout_bg" >
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_top_bg"
android:padding="10dp"
android:hint="Username"
android:textColorHint="#cccccc"
android:drawableLeft="@drawable/user" />
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/edittext_bottom_bg"
android:layout_marginTop="-2dp"
android:padding="10dp"
android:hint="Password"
android:singleLine="true"
android:textColorHint="#cccccc"
android:drawableLeft="@drawable/password"
android:password="true"
android:imeOptions="actionSend" />
<Button
android:id="@+id/submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_margin="4dp"
android:text="Submit"
style="@style/DefaultButtonText"
android:background="@drawable/button_default_bg" />
答案 0 :(得分:1)
是的,找到对已添加android:imeOptions="actionSend"
的EditText的引用,并设置onEditorActionListener以触发要触发的登录方法或按钮。
EditText yourEditText = findViewById(R.id.password);
yourEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_SEND) {
login(); // YOUR LOGIN METHOD HERE
return true;
}
return false;
}
});
答案 1 :(得分:1)
这可以使用以下api完成: android:imeActionId具有以下属性android:imeOptions。将其与setOnEditorActionListener()方法
一起使用此示例也可能对您有所帮助:http://developer.android.com/guide/topics/ui/controls/text.html#ActionEvent
EditText editText = (EditText) findViewById(R.id.search);
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
boolean handled = false;
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessage();
handled = true;
}
return handled;
}
});