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);//        }//    }}

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

下载源码点击这里

更多相关文章

  1. Android学习随笔之使用限定符
  2. 一文学会所有 Android(安卓)TextView 属性,妈妈再也不担心我的代
  3. Android实验——实验1 用户界面:线性布局
  4. 【微信分享】Android、IOS分享图标无法显示解决方法
  5. Android定制:修改开机启动画面
  6. Android(安卓)一个简易的自定义软键盘
  7. [Android]用户界面设计
  8. android merge标签
  9. Android流式布局FlowLayout的实现,Android布局的内部机制onMeasur

随机推荐

  1. 创建虚拟机时报错 Please file a bug aga
  2. Android(安卓)用户界面---样式和主题(Styl
  3. meego的皮 -- QT Quick
  4. 修改一个值,让ScrollView、ListView充满弹
  5. Android(安卓)API个人库的建设V1.0 【UI
  6. 国行Android手机使用google全套GMS服务小
  7. Android(安卓)Studio如何查看自己当前在
  8. android自动安装驱动
  9. Android(安卓)Fragment 详解
  10. 【Android】RxJava2+Retrofit2+OkHttp3的