android软键盘弹出引起的各种不适终极解决方案

fmms 13年前
     <p>        很多写登录界面的开发者都会遇到一个问题:那就是在登录界面时,当你点击输入框时,下边的按钮有时会被输入框挡住,这个不利于用户的体验,所以很多人希望软键盘弹出时,也能把按钮挤上去。很多开发者想要监听键盘的状态,这无疑是一个很麻烦的做法。</p>    <p>       我们可以在AndroidManifest.xml的Activity设置属性:android:windowSoftInputMode = "adjustResize" ,软键盘弹出时,要对主窗口布局重新进行布局,并调用onSizeChanged方法,切记一点当我们设置为“adjustResize”时,我们的界面不要设置为全屏模式,否则设置了这个属性也不会有什么效果。而当我们设置android: windowSoftInputMode = "adjustPan"时,主窗口就不会调用onSizeChanged方法,界面的一部分就会被软键盘覆盖住,就不会被挤到软键盘之上了。</p>    <p>我们通过一段代码来测试一下,当我们设置了该属性后,弹出输入法时,系统做了什么:</p>    <p>重写Layout布局:</p>    <pre class="brush:java; toolbar: true; auto-links: false;">    public class ResizeLayout extends LinearLayout{          private static int count = 0;                    public ResizeLayout(Context context, AttributeSet attrs) {              super(context, attrs);          }                    @Override          protected void onSizeChanged(int w, int h, int oldw, int oldh) {                  super.onSizeChanged(w, h, oldw, oldh);                            Log.e("onSizeChanged " + count++, "=>onResize called! w="+w + ",h="+h+",oldw="+oldw+",oldh="+oldh);          }                    @Override          protected void onLayout(boolean changed, int l, int t, int r, int b) {              super.onLayout(changed, l, t, r, b);              Log.e("onLayout " + count++, "=>OnLayout called! l=" + l + ", t=" + t + ",r=" + r + ",b="+b);          }                    @Override          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {              super.onMeasure(widthMeasureSpec, heightMeasureSpec);                            Log.e("onMeasure " + count++, "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec);          } </pre>    <p></p>    <pre>                    <ol class="dp-j">                         <li class="alt">                              <div class="dp-highlighter bg_java">                                   <div class="bar">                                        <div class="tools">                                             <b>[java]</b>                                              <a class="ViewSource" title="view plain" href="/misc/goto?guid=4959517545212822818">view plain</a>                                             <a class="CopyToClipboard hover" title="copy" href="/misc/goto?guid=4959517545212822818">copy</a>                                             <a class="PrintSource" title="print" href="/misc/goto?guid=4959517545212822818">print</a>                                             <a class="About" title="?" href="/misc/goto?guid=4959517545212822818">?</a>                                        </div>                                    </div>                                    <ol class="dp-j">                                        <li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> ResizeLayout </span><span class="keyword">extends</span><span> LinearLayout{   </span></span> </li>                                        <li><span>    <span class="keyword">private</span><span> </span><span class="keyword">static</span><span> </span><span class="keyword">int</span><span> count = </span><span class="number">0</span><span>;   </span></span> </li>                                        <li class="alt"><span>       </span> </li>                                        <li><span>    <span class="keyword">public</span><span> ResizeLayout(Context context, AttributeSet attrs) {   </span></span> </li>                                        <li class="alt"><span>        <span class="keyword">super</span><span>(context, attrs);   </span></span> </li>                                        <li><span>    }   </span> </li>                                        <li class="alt"><span>       </span> </li>                                        <li><span>    <span class="annotation">@Override</span><span>   </span></span> </li>                                        <li class="alt"><span>    <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> onSizeChanged(</span><span class="keyword">int</span><span> w, </span><span class="keyword">int</span><span> h, </span><span class="keyword">int</span><span> oldw, </span><span class="keyword">int</span><span> oldh) {       </span></span> </li>                                        <li><span>        <span class="keyword">super</span><span>.onSizeChanged(w, h, oldw, oldh);   </span></span> </li>                                        <li class="alt"><span>           </span> </li>                                        <li><span>        Log.e(<span class="string">"onSizeChanged "</span><span> + count++, </span><span class="string">"=>onResize called! w="</span><span>+w + </span><span class="string">",h="</span><span>+h+</span><span class="string">",oldw="</span><span>+oldw+</span><span class="string">",oldh="</span><span>+oldh);   </span></span> </li>                                        <li class="alt"><span>    }   </span> </li>                                        <li><span>       </span> </li>                                        <li class="alt"><span>    <span class="annotation">@Override</span><span>   </span></span> </li>                                        <li><span>    <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> onLayout(</span><span class="keyword">boolean</span><span> changed, </span><span class="keyword">int</span><span> l, </span><span class="keyword">int</span><span> t, </span><span class="keyword">int</span><span> r, </span><span class="keyword">int</span><span> b) {   </span></span> </li>                                        <li class="alt"><span>        <span class="keyword">super</span><span>.onLayout(changed, l, t, r, b);   </span></span> </li>                                        <li><span>        Log.e(<span class="string">"onLayout "</span><span> + count++, </span><span class="string">"=>OnLayout called! l="</span><span> + l + </span><span class="string">", t="</span><span> + t + </span><span class="string">",r="</span><span> + r + </span><span class="string">",b="</span><span>+b);   </span></span> </li>                                        <li class="alt"><span>    }   </span> </li>                                        <li><span>       </span> </li>                                        <li class="alt"><span>    <span class="annotation">@Override</span><span>   </span></span> </li>                                        <li><span>    <span class="keyword">protected</span><span> </span><span class="keyword">void</span><span> onMeasure(</span><span class="keyword">int</span><span> widthMeasureSpec, </span><span class="keyword">int</span><span> heightMeasureSpec) {   </span></span> </li>                                        <li class="alt"><span>        <span class="keyword">super</span><span>.onMeasure(widthMeasureSpec, heightMeasureSpec);   </span></span> </li>                                        <li><span>           </span> </li>                                        <li class="alt"><span>        Log.e(<span class="string">"onMeasure "</span><span> + count++, </span><span class="string">"=>onMeasure called! widthMeasureSpec="</span><span> + widthMeasureSpec + </span><span class="string">", heightMeasureSpec="</span><span> + heightMeasureSpec);   </span></span> </li>                                        <li><span>    }   </span> </li>                                    </ol>                               </div> </li>                         <li> </li>                     </ol> </pre>我们的布局设置为:    <pre class="brush:xml; toolbar: true; auto-links: false;">    <com.winuxxan.inputMethodTest.ResizeLayout           xmlns:android="http://schemas.android.com/apk/res/android"          android:id="@+id/root_layout"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:orientation="vertical"          >                    <EditText              android:layout_width="fill_parent"               android:layout_height="wrap_content"           />                  <LinearLayout                  android:id="@+id/bottom_layout"                  android:layout_width="fill_parent"                   android:layout_height="fill_parent"                   android:orientation="vertical"                  android:gravity="bottom">s                   <TextView                android:layout_width="fill_parent"               android:layout_height="wrap_content"               android:text="@string/hello"              android:background="#77777777"            />         </LinearLayout>      </com.winuxxan.inputMethodTest.ResizeLayout> </pre>AndroidManifest.xml的Activity设置属性:android:windowSoftInputMode = "adjustResize"    <br />     运行程序,点击文本框,查看调试信息:    <br />     E/onMeasure 6(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742024    <br />     E/onMeasure 7(7960): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec = 1073742025    <br />     E/onSizeChanged 8(7960): =>onSizeChanged called! w=320,h=201,oldw=320,oldh=377    <br />     E/onLayout 9(7960): =>OnLayout called! l=0, t=0,r=320,b=201    <br />     从调试结果我们可以看出,当我们点击文本框后,根布局调用了onMeasure,onSizeChanged和onLayout。    <p>  windowSoftInputMode的值如果设置为adjustPan,那么该Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作。<br />     上面的例子中,我们将AndroidManifest.xml的属性进行更改:android: windowSoftInputMode = "adjustPan"<br /> <br />     重新运行,并点击文本框,查看调试信息:<br />     E/onMeasure 6(8378): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742200<br />     E/onMeasure 7(8378): =>onMeasure called! widthMeasureSpec=1073742144, heightMeasureSpec=1073742201<br />     E/onLayout 8(8378): =>OnLayout called! l=0, t=0,r=320,b=377<br />     我们看到:系统也重新进行了measrue和layout,但是我们发现,layout过程中onSizeChanged并没有调用,这说明输入法弹出前后并没有改变原有布局的大小。</p>    <p>当然还有其他属性可以设置:</p>    <p><span style="font-family:微软雅黑;">"stateUnspecified"</span></p>    <p><span style="font-family:微软雅黑;">软键盘的状态(是否它是隐藏或可见)没有被指定。系统将选择一个合适的状态或依赖于主题的设置。</span></p>    <p><span style="font-family:微软雅黑;">这个是为了软件盘行为默认的设置。</span></p>    <p><span style="font-family:微软雅黑;">"stateUnchanged"</span></p>    <p><span style="font-family:微软雅黑;">软键盘被保持无论它上次是什么状态,是否可见或隐藏,当主窗口出现在前面时。</span></p>    <p><span style="font-family:微软雅黑;">"stateHidden"</span></p>    <p><span style="font-family:微软雅黑;">当用户选择该Activity时,软键盘被隐藏——也就是,当用户确定导航到该Activity时,而不是返回到它由于离开另一个Activity。</span></p>    <p><span style="font-family:微软雅黑;">"stateAlwaysHidden"</span></p>    <p><span style="font-family:微软雅黑;">软键盘总是被隐藏的,当该Activity主窗口获取焦点时。</span></p>    <p><span style="font-family:微软雅黑;">"stateVisible"</span></p>    <p><span style="font-family:微软雅黑;">软键盘是可见的,当那个是正常合适的时(当用户导航到Activity主窗口时)。</span></p>    <p><span style="font-family:微软雅黑;">"stateAlwaysVisible"</span></p>    <p><span style="font-family:微软雅黑;">当用户选择这个Activity时,软键盘是可见的——也就是,也就是,当用户确定导航到该Activity时,而不是返回到它由于离开另一个Activity。</span></p>    <p><span style="font-family:微软雅黑;">"adjustUnspecified"</span></p>    <p><span style="font-family:微软雅黑;">它不被指定是否该Activity主窗口调整大小以便留出软键盘的空间,或是否窗口上的内容得到屏幕上当前的焦点是可见的。系统将自动选择这些模式中一种主要依赖于是否窗口的内容有任何布局视图能够滚动他们的内容。如果有这样的一个视图,这个窗口将调整大小,这样的假设可以使滚动窗口的内容在一个较小的区域中可见的。这个是主窗口默认的行为设置。</span></p>    <p><span style="font-family:微软雅黑;">"adjustResize"</span></p>    <p><span style="font-family:微软雅黑;">该Activity主窗口总是被调整屏幕的大小以便留出软键盘的空间</span></p>    <p><span style="font-family:微软雅黑;">"adjustPan"</span></p>    <p><span style="font-family:微软雅黑;">该Activity主窗口并不调整屏幕的大小以便留出软键盘的空间。相反,当前窗口的内容将自动移动以便当前焦点从不被键盘覆盖和用户能总是看到输入内容的部分。这个通常是不期望比调整大小,因为用户可能关闭软键盘以便获得与被覆盖内容的交互操作。</span></p>    <p></p>