Android输入法 监听事件
登录界面有一个输入用户名和密码的编辑框:
private EditText et_userName;// 账户 private EditText et_password;// 密码
布局文件如下:
<EditText android:id="@+id/login_et_username" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:drawableLeft="@drawable/account_icon" android:drawablePadding="7dp" android:hint="@string/login_num" android:paddingLeft="15dp" android:singleLine="true" android:textColor="@android:color/white" android:textColorHint="@color/login_frame_hint_color" android:textSize="20sp" /> <EditText android:id="@+id/login_et_userpassword" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:background="@null" android:drawableLeft="@drawable/password_icon" android:drawablePadding="7dp" android:hint="@string/login_password" android:inputType="textPassword" android:imeOptions="actionGo" android:paddingLeft="15dp" android:singleLine="true" android:textColor="@android:color/white" android:textColorHint="@color/login_frame_hint_color" android:textSize="20sp" />
Acvity中实现监听输入法回车键执行登录操作:
//密码文本框的监听
et_password.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//判断action为GO执行的操作 if(actionId == EditorInfo.IME_ACTION_GO){ InputMethodManager imm = (InputMethodManager) v .getContext().getSystemService( Context.INPUT_METHOD_SERVICE); if (imm.isActive()) { imm.hideSoftInputFromWindow( v.getApplicationWindowToken(), 0); } //执行登录方法,提交表单操作。 return true; } return false; } });