您的位置:首页 > 移动开发 > Android开发

Android软键盘相关的知识

2015-06-19 14:05 344 查看
1、收起某Activity中的软键盘

[code] 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

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


Java代码对应EditorInfo.IME_ACTION_XXX

如果设置了imeOptions但是未起作用:

解决方法:

在EditText中:

1 将singleLine设置为true

2 将inputType设置为text

[code]<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代码:

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


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

[code]    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;
                    }
                }
        );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: