android软键盘上添加一个按钮

android软键盘上添加一个按钮:

  • 第一种

    我们在根布局的最底部添加一个按钮,利用布局中的ScrollView在软键盘弹出的时候
    将底部的按钮挤压到软键盘上边,先看效果图:
    这里写图片描述

    看代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.yzhx.testsoftinput.ByScrollviewActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />
        </RelativeLayout>
    </ScrollView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_alignParentBottom="true">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="30dp"
            android:background="#cccccc"
            android:gravity="center"
            android:text="按下开始录音"
            android:textColor="#ff6600"
            android:textSize="18sp"/>
    </RelativeLayout>
</RelativeLayout>
注意:ScrollView中不能去掉滚动条(scrollbars),一旦添加了下边这句话将滚动条去掉了,

布局里边的内容便不会被软键盘挤压上去.!

android:scrollbars="none"
  • 第二种 软键盘弹出自动调整高度 效果通上边
    在对应的activity在清单文件中注册的时候添加以下代码:
android:windowSoftInputMode="stateVisible|adjustResize"
  • 第三种 监听软键盘的状态,当软键盘显示的时候显示布局,软键盘隐藏的时候隐藏布局
    这里写图片描述
    看了下百度地图和京东(ios版)的语音搜索中都是软键盘显示的时候
    显示布局,软键盘隐藏的时候隐藏布局.
    原理:监听软键盘的状态,根据软键盘的高度来显示或者隐藏按钮.

    布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:id="@+id/rl_root"
    android:layout_height="match_parent"
    tools:context="com.yzhx.testsoftinput.BySoftInputListnerActivity">

    <EditText
        android:id="@+id/et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Hello World!" />

    <Button
        android:layout_below="@id/et"
        android:text="搜索"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

主界面:

public class BySoftInputListnerActivity extends AppCompatActivity {

    @ViewInject(R.id.rl_root)
    private RelativeLayout rl_root;

    @ViewInject(R.id.et)
    private EditText et;

    private View view;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_by_soft_input_listner);
        ViewUtils.inject(this);
        et.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

            //当键盘弹出隐藏的时候会 调用此方法。
            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                //获取当前界面可视部分
                BySoftInputListnerActivity.this.getWindow().getDecorView().getWindowVisibleDisplayFrame(r);
                //获取屏幕的高度
                int screenHeight = BySoftInputListnerActivity.this.getWindow().getDecorView().getRootView().getHeight();
                //此处就是用来获取键盘的高度的, 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
                int heightDifference = screenHeight - r.bottom;
                Log.d("Keyboard Size", "Size: " + heightDifference);
                showAViewOverKeyBoard(heightDifference);
            }

        });
    }

    private void showAViewOverKeyBoard(int heightDifference) {
        if (heightDifference > 0) {//显示
            if (view == null) {//第一次显示的时候创建  只创建一次
                view = View.inflate(this, R.layout.item, null);
                RelativeLayout.LayoutParams loginlayoutParams = new RelativeLayout.LayoutParams(-1, -2);
                loginlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                loginlayoutParams.bottomMargin = heightDifference;
                rl_root.addView(view, loginlayoutParams);
            }
            view.setVisibility(View.VISIBLE);
        } else {//隐藏
            if (view != null) {
                view.setVisibility(View.GONE);
            }
        }
    }
//    private void showAViewOverKeyBoard(int heightDifference) {
//        if (view == null) {
//            view = View.inflate(this, R.layout.item, null);
//        }
//        RelativeLayout.LayoutParams loginlayoutParams = new RelativeLayout.LayoutParams(-1, -2);
//        loginlayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
//        loginlayoutParams.bottomMargin = heightDifference;
//        rl_root.removeView(view);
//        if (heightDifference > 0) {
//            rl_root.addView(view, loginlayoutParams);
//        }
//    }
}

注释掉的方法是不想每次软键盘显示得的时候都去添加.

下载源码点击这里

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值