android 回车键事件编程
默认情况下软键盘右下角的按钮为“下一个”,点击会到下一个输入框,保持软键盘
设置 android:imeOptions="actionDone" ,软键盘下方变成“完成”,点击后光标保持在原来的输入框上,并且软键盘关闭
- package com.test;
- import com.test.main.TestAsyn;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.KeyEvent;
- import android.view.inputmethod.EditorInfo;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.TextView.OnEditorActionListener;
- import android.widget.Toast;
- public class IMFActivity extends Activity implements OnEditorActionListener {
- EditText etDefault;
- EditText etEmail;
- EditText etNumber;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.imf_layout);
- etDefault = (EditText)findViewById(R.id.default_content);
- etEmail = (EditText)findViewById(R.id.email_content);
- etNumber = (EditText)findViewById(R.id.number_content);
- etDefault.setOnEditorActionListener(this);
- etEmail.setOnEditorActionListener(this);
- etNumber.setOnEditorActionListener(this);
- }
- @Override
- public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
- switch(actionId){
- case EditorInfo.IME_NULL:
- System.out.println("null for default_content: " + v.getText() );
- break;
- case EditorInfo.IME_ACTION_SEND:
- System.out.println("action send for email_content: " + v.getText());
- break;
- case EditorInfo.IME_ACTION_DONE:
- System.out.println("action done for number_content: " + v.getText());
- break;
- }
- //Toast.makeText(this, v.getText()+"--" + actionId, Toast.LENGTH_LONG).show();
- return true;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent" android:layout_height="fill_parent">
- <TableLayout android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TableRow>
- <TextView android:text="No special rules" android:id="@+id/TextView01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
- <EditText android:text="1111111111111" android:id="@+id/default_content"
- android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText>
- </TableRow>
- <TableRow>
- <TextView android:text="Email address:" android:id="@+id/TextView01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
- <EditText android:text="" android:id="@+id/email_content"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:inputType="text|textEmailAddress"
- android:imeOptions="actionSend"></EditText>
- </TableRow>
- <TableRow>
- <TextView android:text="Signed decimal number:" android:id="@+id/TextView01"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
- <EditText android:text="" android:id="@+id/number_content"
- android:layout_width="fill_parent" android:layout_height="wrap_content"
- android:inputType="number|numberSigned|numberDecimal"
- android:imeOptions="actionDone"></EditText>
- </TableRow>
- </TableLayout>
- </ScrollView>
http://www.apkbus.com/forum.php?mod=viewthread&tid=45005
实现android按下回车键便隐藏输入键盘,有两种方法: 1.)如果布局是多个EditText,为每个EditText控件设置android:singleLine=”true”,弹出的软盘输入法中回车键为next,直到最后一个获取焦点后显示为Done,点击Done后,软盘输入键盘便隐藏。或者将EditText的imeOptions属性设置android:imeOptions=”actionDone”,则不管是不是最后一个EditText,点击回车键即隐藏输入法。 2.)监听Enter的事件,编写Enter的事件响应。设置文本框的OnKeyListener,当keyCode ==KeyEvent.KEYCODE_ENTER的时候,表明Enter键被按下,就可以编写自己事件响应功能了。 代码源:<ignore_js_op> EnterListenter.rar 具体代码:
图-1 点击回车键隐藏键盘 PS: 把EditText的Ime Options属性设置成不同的值,Enter键上可以显示不同的文字或图案
searchEdit.setOnEditorActionListener(new OnEditorActionListener() { @Override if (imm.isActive()) { break; |