Android 开发学习进程0.29 软键盘使用问题
发布日期:2021-05-09 00:48:52 浏览次数:16 分类:技术文章

本文共 2132 字,大约阅读时间需要 7 分钟。

软键盘使用问题

软键盘在edittext获取焦点后自动弹出,需要设置监听:

etActsearchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {            @Override            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {                if (actionId == EditorInfo.IME_ACTION_SEARCH) {                    // close soft keyboard                    InputMethodManager inputMethodManager = (InputMethodManager) v.getContext()                            .getSystemService(Context.INPUT_METHOD_SERVICE);                    if (inputMethodManager.isActive()) {                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);                    }                    search(etActsearchInput.getText().toString());                    return true;                }                return false;            }        });

以上实现了监听 软键盘的确定键,同时 edittext布局文件同样需设置

android:singleLine="true"android:imeOptions="actionSearch"

imeOptions属性决定了软键盘右下角按键文字,属性包括搜索、确定、换行。同时由edit源码知:调用软键盘与关闭软键盘是相似的,需要分两步:

1、获取InputMethodManager 实例
2、调用showSoftInput(xx)
非在editview中使用软键盘时,如button设置Button.setFocusableInTouchMode(true)
设置弹出软键盘时activity布局是否被顶上去
getWindow().getAttributes().softInputMode = WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN;
或是在manifest文件中设置android:windowSoftInputMode="adjustResize"
部分app有 activity的window不往上移动,而editview移动至软键盘上,这样实现的原理是,需要设置属性
android:windowSoftInputMode="adjustResize|stateAlwaysHidden"
同时计算软键盘高度,移动editview坐标。点击外部隐藏软键盘:

@Override    public boolean dispatchTouchEvent(MotionEvent ev) {        if (ev.getAction() == MotionEvent.ACTION_DOWN) {              //获取焦点View              View v = getCurrentFocus();            if (isFocusEditText(v, hideSoftByEditViewIds())) {                //如果焦点在editText上                //隐藏键盘                KeyBoardUtils.hideInputForce(this);                //清除焦点                clearViewFocus(v, hideSoftByEditViewIds());            }        }        return super.dispatchTouchEvent(ev);    }

可以选择id过滤的函数:

@Override    public int[] hideSoftByEditViewIds() {        int[] ids = {R.id.et_phone, R.id.et_check_code, R.id.et_city_code};        return ids;    }
上一篇:Android 开发学习进程0.30 builder模式创建popwindow
下一篇:Github 连接不上解决方法 Android studio 阿里镜像地址

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月21日 05时21分02秒