2014-06-27 81 views
0

有没有办法让Enter键提交我的表单?现在我有一个登录按钮,使用addListenerButton()发送......我可以以某种方式将Enter键链接到该按钮吗?我认为我的.java代码是不相关的,但如果需要,我总是可以发布它。Android - 有IME输入密钥提交?

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" /> 

回答

1

是,找到你的EditText参考您已加入android:imeOptions="actionSend"并设置onEditorActionListener触发要触发登录的方法或按钮的onClick 。 android:imeActionId具有以下属性android:imeOptions

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

这可以通过以下API来完成。与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; 
    } 
});