判斷軟鍵盤的彈出

未彈出軟鍵盤時的佈局,很簡單,只有一個webview加一個底部bar,底部bar由一個linearlayout包含四個button組成。android

當佈局中有webview時,點擊webview上的輸入框,會有軟鍵盤彈出以輸入文字。web

問題:此時,若是佈局含有底部bar,底部bar會被軟鍵盤托起。以下圖所示:

 

解決方式:

使用  RelativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(listener);監聽軟鍵盤是否彈出,在彈出時隱藏底部bar便可。網絡

注意:Manifest.xml中須要在Activity中添加android:theme="@android:style/Theme.Light.NoTitleBar",否則會有問題。ide

由於是webview須要鏈接網絡,因此,Manifest.xml中也要添加INTENET權限。佈局

public class MainActivity extends Activity {
    RelativeLayout re;
    LinearLayout footBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        WebView wb = (WebView) findViewById(R.id.webView);
        re = (RelativeLayout) findViewById(R.id.relayout);
        footBar = (LinearLayout) findViewById(R.id.footBar);
        wb.getSettings().setJavaScriptEnabled(true);
        wb.loadUrl("http://www.baidu.com");

        re.getViewTreeObserver().addOnGlobalLayoutListener(
                new OnGlobalLayoutListener() {

                    @Override
                    public void onGlobalLayout() {
                        int heightDiff = re.getRootView().getHeight()
                                - re.getHeight();

                        if (heightDiff > 100) {
                            // 說明鍵盤是彈出狀態
                            footBar.setVisibility(View.GONE);
                        } else {
                            footBar.setVisibility(View.VISIBLE);
                        }

                    }
                });
    }
}

xml:activity_mainspa

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <WebView
        android:id="@+id/webView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/footBar" />

    <LinearLayout
        android:id="@+id/footBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" >

        <Button
            android:id="@+id/homeBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_weight="1" 
            android:text="Button1"/>

        <Button
            android:id="@+id/backBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_weight="1" 
            android:text="Button2"/>

        <Button
            android:id="@+id/forwardBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_weight="1"
            android:text="Button3" />

        <Button
            android:id="@+id/reloadBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_weight="1" 
            android:text="Button4"/>
    </LinearLayout>

</RelativeLayout>

 解決以後:.net

 

源碼下載地址:http://download.csdn.net/detail/bx276626237/88503573d

相關文章
相關標籤/搜索