判断软键盘的弹出

未弹出软键盘时的布局,很简单,只有一个webview加一个底部bar,底部bar由一个linearlayout包含四个button组成。

当布局中有webview时,点击webview上的输入框,会有软键盘弹出以输入文字。

问题:此时,如果布局含有底部bar,底部bar会被软键盘托起。如下图所示:

 

解决方式:

使用  RelativeLayout.getViewTreeObserver().addOnGlobalLayoutListener(listener);监听软键盘是否弹出,在弹出时隐藏底部bar即可。

注意:Manifest.xml中需要在Activity中添加android:theme="@android:style/Theme.Light.NoTitleBar",不然会有问题。

因为是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_main

<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>

 解决之后:

 

源码下载地址:http://download.csdn.net/detail/bx276626237/8850357

转载于:https://www.cnblogs.com/Cherry-B/p/4607407.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值