android 在输入EditText是软件键盘挡住输入框解决方案

来源:互联网 发布:python 加减 编辑:程序博客网 时间:2024/05/17 03:56

如果在Activity中的布局的下方有EditText,获取焦点弹出软键盘的时候,如果不做处理,软键盘可能会遮挡输入框,用户提现不好,网上也有很多人提出结局方案,这里就分析一下解决的效果.

需要用到EditText的布局大概分为两种,一种是页面显示的内容不希望有残缺,比如软键盘把标题挤出了屏幕,如登录注册搜索等,EditText多半在页面上半部分.另一种是页面的上半部分不重要,比如聊天窗口,软键盘弹出的时候,上半部分的聊天记录可以被基础屏幕外,EditText多半在最下方.其他的情况可以根据本文做出选择,结果在最下面.

开始:

网上针对这个问题解决的方法大概这么几种:

1.修改AndroidManifest.xml文件

2.在Activity中添加配置

3.在布局文件中添加ScrollView

4.在布局文件中添加ScrollView,并且在activity中加入一个方法

1.修改AndroidManifest.xml文件

在AndroidManifest.xml中对应的Activity配置:Android:windowSoftInputMode="stateVisible|adjustResize"

<code class="xml"><span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">activity</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">".LoginActivity"</span>            //显示软键盘,并让布局压缩            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:windowSoftInputMode</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"stateVisible|adjustResize"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:screenOrientation</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"portrait"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">activity</span>></span></code>

含义:该页面在弹出软键盘的同时屏幕内容被压缩,以腾出软键盘的空间.

解释它们的含义:

  • stateUnspecified:界面没有设置"android:windowSoftInputMode"时的状态.这个状态是弹出有EditText的界面时时不弹出软键盘的,当EditText获取焦点的时候弹出软件盘
  • stateUnchanged:状态不改变,意思就是和上一个界面相同,上一个界面弹出软键盘,跳转到这个界面时,软键盘也是弹出状态.
  • stateHidden:隐藏键盘,弹出这个界面的时候,不管上个界面是什么状态,这个界面的软键盘都是隐藏的.
  • stateAlwaysHidden:一直隐藏(跟上面的区别未知,试不出来).
  • stateVisible:强制弹出软键盘.跳转到界面后,没有EditText也弹出键盘.
  • stateAlwaysVisible:一直显示键盘(同上).

这里开始就是跟内容相关的了,也跟本文也相关

  • adjustUnspecified:算是默认方式.如果界面没有被ScrowView包裹,键盘会挡住一部分内容,如果界面被ScrowView包裹了,会让ScrowView中的内容滚动,以放下软键盘.
  • adjustResize:主界面会缩放,用来放置软键盘(见[图1][图2]).

图1

图2
  • adjustPan:主界面不会缩放,会向上移来放置软键盘(见[图3][图4]).

图3

图4

上面两个不同的地方在于,如果内容是listview,当现实最下面一个item后弹出软键盘,adjustResize会让listview下沿被软键盘挡住,adjustPan则还是现实最下面一个item.

2.在Activity中添加配置

在该Activity中的onCretae()的setContentView()方法前面添加:getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN)

含义:把整个Layout顶上去露出获得焦点的EditText,不压缩多余空间.

解释他们的含义:

  • SOFT_INPUT_ADJUST_NOTHING: 不调整(输入法完全直接覆盖住,未开放此参数);
  • SOFT_INPUT_ADJUST_PAN:把整个Layout顶上去露出获得焦点的EditText,不压缩多余空间
  • SOFT_INPUT_ADJUST_RESIZE: 整个Layout重新编排,重新分配多余空间;
  • SOFT_INPUT_ADJUST_UNSPECIFIED: 系统自己根据内容自行选择上两种方式的一种执行(默认配置).

这里SOFT_INPUT_ADJUST_RESIZE和SOFT_INPUT_ADJUST_PAN和上面中的adjustResize和adjustPan效果是一样的.

<code class="java"><span class="hljs-annotation" style="color: rgb(155, 133, 157);">@Overrideprotected</span> <span class="hljs-function"><span class="hljs-keyword" style="color: rgb(0, 0, 136);">void</span> <span class="hljs-title">onCreate</span><span class="hljs-params" style="color: rgb(102, 0, 102);">(@Nullable Bundle savedInstanceState)</span> </span>{    <span class="hljs-keyword" style="color: rgb(0, 0, 136);">super</span>.onCreate(savedInstanceState);    <span class="hljs-comment" style="color: rgb(136, 0, 0);">//让布局向上移来显示软键盘</span>    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);    setContentView(R.layout.activity_login);    init();}</code>

3.在布局文件中添加ScrollView

ScrollView是能让弹出的软键盘添加到布局的下面,从而不挡住部分布局.这里往往需要跟上面的adjustResize和adjustPan两个状态一起用.
布局:

<code class="xml"><span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">ScrollView</span>    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"0dp"</span>    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_weight</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"8"</span>    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:fillViewport</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"true"</span>></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">LinearLayout</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:orientation</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"vertical"</span>></span>        <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">ListView</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/lv"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"0dp"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_weight</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"1"</span> /></span>        <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">EditText</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"50dp"</span> /></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">LinearLayout</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">ScrollView</span>></span></code>

向上移的方式 设置:

<code class="xml"><span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">activity</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">".SecondActivity"</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:windowSoftInputMode</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"adjustPan"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">activity</span>></span></code>

压缩的方式 设置:

<code class="xml">  <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">activity</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:name</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">".SecondActivity"</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:windowSoftInputMode</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"adjustResize"</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">activity</span>></span></code>

4.在布局文件中添加ScrollView

ScrollView是能让弹出的软键盘添加到布局的下面,从而不挡住部分布局.这里往往需要跟上面的adjustResize和adjustPan两个状态一起用.
布局:

<code class="xml"><span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">ScrollView</span>    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"0dp"</span>    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_weight</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"8"</span>    <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:fillViewport</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"true"</span>></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">LinearLayout</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>        <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:orientation</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"vertical"</span>></span>        <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">ListView</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:id</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"@+id/lv"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"0dp"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_weight</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"1"</span> /></span>        <span class="hljs-tag" style="color: rgb(0, 102, 102);"><<span class="hljs-title" style="color: rgb(0, 0, 136);">EditText</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_width</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"match_parent"</span>            <span class="hljs-attribute" style="color: rgb(102, 0, 102);">android:layout_height</span>=<span class="hljs-value" style="color: rgb(0, 136, 0);">"50dp"</span> /></span>    <span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">LinearLayout</span>></span><span class="hljs-tag" style="color: rgb(0, 102, 102);"></<span class="hljs-title" style="color: rgb(0, 0, 136);">ScrollView</span>></span></code>

在activity中调用:

<code class="xml"><span class="hljs-tag" style="color: rgb(0, 102, 102);"></span>private void controlKeyboardLayout(final ScrollView root, final Activity context) {        root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver                .OnGlobalLayoutListener() {            @Override            public void onGlobalLayout() {                Rect rect = new Rect();                root.getWindowVisibleDisplayFrame(rect);                int rootInvisibleHeight = root.getRootView().getHeight() - rect.bottom;                //若不可视区域高度大于100,则键盘显示                if (rootInvisibleHeight > 100) {                    int[] location = new int[2];                    View focus = context.getCurrentFocus();                    if (focus != null) {                        focus.getLocationInWindow(location);                        int scrollHeight = (location[1] + focus.getHeight()) - rect.bottom;                        if (rect.bottom < location[1] + focus.getHeight()) {                            root.scrollTo(0, scrollHeight);                        }                    }                } else {                    //键盘隐藏                    root.scrollTo(0, 0);                }            }        });    }<span class="hljs-tag" style="color: rgb(0, 102, 102);"></span></code>

0 0
原创粉丝点击