Android软键盘相关的知识

3 篇文章 0 订阅
1 篇文章 0 订阅

1、收起某Activity中的软键盘

 public static void hideIMEInThisActivity(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view == null) {
            Log.d("", "没找到焦点view");
            return;
        }
        ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(
                activity.getCurrentFocus()
                        .getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS
        );
    }

2、设置软键盘右下角的按钮显示图标及操作
利用android:imeOptions

//完成
android:imeOptions="actionDone"
//发送
android:imeOptions="actionSend"
//搜索
android:imeOptions="actionSearch"
//等等

Java代码对应EditorInfo.IME_ACTION_XXX

如果设置了imeOptions但是未起作用:
解决方法:
在EditText中:

1 将singleLine设置为true
2 将inputType设置为text

<EditText
             android:id="@+id/edit_content"
             android:hint="@string/search"
             android:gravity="center_vertical"
             android:layout_width="match_parent"
             android:layout_height="match_parent"
                 android:singleLine="true"
             android:imeOptions="actionSearch"
                 />

Java代码:

editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
editText.setInputType(EditorInfo.TYPE_CLASS_TEXT);
editText.setSingleLine(true);

3、对于EditText监听软键盘的‘搜索’‘Enter’等操作

    editText.setOnEditorActionListener(
                new TextView.OnEditorActionListener() {
                    @Override
                    public boolean onEditorAction(TextView v,
                                                  int actionId, 
                                                  KeyEvent event) {
                        switch (actionId) {
                            case EditorInfo.IME_ACTION_SEARCH:
                                performSearch();
                                return true;
                        }
                        return false;
                    }
                }
        );
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杨筱毅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值