Android开发之软键盘遮盖EditText

来源:互联网 发布:物品记录软件 编辑:程序博客网 时间:2024/05/21 11:09

写下此博客,是为了分享我遇到的使用多个EditText,软键盘弹出遮挡了输入框。

 网上搜索了一大堆,基本都是说使用android:windowSoftInputMode="adjustPan" 就可以,但是此方法遇到按屏幕尺寸分比例的时候就行不通!

操作如下:

点击EditText,然后再点击屏幕空白处,软键盘隐藏,然后再点击同一个EditText,此时弹出的软键盘会遮盖出EditText。


下面说说我的解决方案


第一步: 

标红部分是必须的新增的,弹出软键盘时,整个窗体往上偏移,而不是使用android:windowSoftInputMode="adjustResize" 那样会压缩控件。


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/rootview_register"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="@color/c1_main"    android:orientation="vertical" >    <span style="background-color: rgb(255, 0, 0);"><ScrollView        android:id="@+id/sv_register_all"        android:layout_width="fill_parent"        android:layout_height="fill_parent" ></span>        <span style="background-color: rgb(255, 0, 0);"><LinearLayout            android:id="@+id/ll_register_all"            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:orientation="vertical" ></span>            <RelativeLayout                android:id="@+id/rl_register_top"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:layout_weight="3" >

第二步:

manifest中的Activity属性

<activity            android:name="com.yjtc.msx.activity.RegistNewActivity"            <span style="background-color: rgb(255, 0, 0);">android:configChanges="orientation|keyboardHidden"</span>            android:screenOrientation="portrait"            <span style="background-color: rgb(255, 0, 0);">android:windowSoftInputMode="adjustUnspecified"</span> />

第三步:

        由于使用了ScrollView,那Activity中点击onTouch事件中的点击空白处隐藏软键盘,就会和Scrollview获取焦点冲突;此时必须监听Scrollview的OnTouchListener

sv_register_all.setOnTouchListener(new View.OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                if (event.getAction() == MotionEvent.ACTION_DOWN) {                    if (getCurrentFocus() != null                            && getCurrentFocus().getWindowToken() != null) {                        manager.hideSoftInputFromWindow(getCurrentFocus()                                .getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);                    }                }                return false;            }        });

到此,就完美解决遮盖问题了。

"——没有拼命的干劲,怎能有精彩的结果!"

版权声明:本文为博主原创文章,未经博主允许不得转载。若有错误地方,还望批评指正,不胜感激。

0 0
原创粉丝点击