【Android】安卓开发实战之软键盘设置

一、设置软键盘的“完成”按键

要将软键盘的可变按键设置为“完成”,只要在EditText布局里加上这句话就可以了。

android:imeOptions="actionDone"
如果你要将可变按键设置为“搜索”,那么还要设置为单行。

android:imeOptions="actionSearch"
android:singleLine="true"
二、点击空白处,收起软键盘

在需要点击空白收回软件盘的Activity中重写onTouchEvent方法即可。

    //---------------------------------点击空白处收回软键盘-----------------------------------------
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        // TODO Auto-generated method stub
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        }
        return super.onTouchEvent(event);
    }
    //----------------------------------------------------------------------------------------------
三、手动收回软键盘

想在特定时候使用代码手动回收软键盘,只要添加如下代码即可

因为我是在Fragment中收回,所以要先获得相应的Activity。

                //----------------------------------收起软键盘--------------------------------------------------------
                UsingMain usingMain = (UsingMain)getActivity();
                InputMethodManager imm = (InputMethodManager)usingMain.getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(usingMain.getWindow().getDecorView().getWindowToken(),0);
                }
                //-----------------------------------------------------------------------------------------------------
实际效果:








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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值