转载请注明出处:https://my.oschina.net/wfy94/blog/819699
使用情景:应用的登陆界面,输入用户名,希望软键盘出现下一步按钮,并跳转到密码框,在密码框希望软键盘出现完成按钮,点击完成提交数据。
布局写法:
imeOptions 指定软键盘额外显示的按钮
imeOptions =actionNext时,需要用nextFocusForward指定下一个获取焦点的view
<EditText android:id="@+id/etv_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@android:color/transparent" android:layout_marginLeft="15dp" android:textColor="@android:color/white" android:textColorHint="@android:color/white" android:hint="请输入帐号" android:imeOptions="actionNext" android:nextFocusForward="@+id/etv_password" android:maxLength="20" android:singleLine="true" android:textSize="@dimen/login_text_size" />
<EditText android:id="@+id/etv_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:background="@android:color/transparent" android:layout_marginLeft="15dp" android:textColor="@android:color/white" android:hint="请输入密码" android:textColorHint="@android:color/white" android:inputType="textPassword" android:imeOptions="actionDone" android:maxLength="20" android:singleLine="true" android:textSize="@dimen/login_text_size" />
代码写法:
监听额外按钮的事件
如果没有达到下一步的条件,监听到下一步之后再次让它获取焦点就可以
TextView.OnEditorActionListener
etv_username.setOnEditorActionListener(this); etv_password.setOnEditorActionListener(this);
@Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { String username=etv_username.getText().toString().trim(); switch (v.getId()){ case R.id.etv_username: if(actionId==EditorInfo.IME_ACTION_NEXT){ if(TextUtils.equals(username,"")){ v.setNextFocusForwardId(v.getId()); Toast.makeText(activity,"请输入帐号",Toast.LENGTH_SHORT).show(); }else{ v.setNextFocusForwardId(R.id.etv_password); } } break; case R.id.etv_password: if(actionId==EditorInfo.IME_ACTION_DONE){ String password=etv_password.getText().toString().trim(); submit(username,password); } break; } return false; }