让EditText响应软键盘动作

转载请注明出处: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;
}

转载于:https://my.oschina.net/wfy94/blog/819699

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值