Android软键盘处理
2016-05-03 19:02
489 查看
在此,不会提出android:windowSoftInputMode这个相关属性,因为网上也比较多。
主要讲解,当android:windowSoftInputMode无效果(全屏或者其他情况)或者不想屏幕变形又不想内容被遮挡的情况。我想处理方式只有一种移动控件又或者滚动控件。
网上有些说在外层添加ScrollView进行滚动个人感觉不是很合适,而且不适用多数场景,因此在这块提一些个人的解决方式,以便帮到大家。
核心代码如下:
布局文件代码:
再次提供demo链接,仅供参考:
下载Demo请点击
有问题请留言,或联系本人Qq:63724830
主要讲解,当android:windowSoftInputMode无效果(全屏或者其他情况)或者不想屏幕变形又不想内容被遮挡的情况。我想处理方式只有一种移动控件又或者滚动控件。
网上有些说在外层添加ScrollView进行滚动个人感觉不是很合适,而且不适用多数场景,因此在这块提一些个人的解决方式,以便帮到大家。
核心代码如下:
private Button moBtnLogin; /** 登陆按钮距底部距离 (此处我们做 软键盘不会遮挡登录按钮的方式)*/ private int mBtnLoginBottomHeight = -1; @SuppressLint("NewApi") private void keyBoardChangedListener() { //根布局 final View decordView = this.getWindow().getDecorView(); //偏移量 //此处数据 请自己添加工具类进行px dp 转化 30给的是px值,可以根据需求修改 final int scaleHeight = 30; //监听布局宽、高等信息,详细用法可自行查找 decordView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() { @Override public void onGlobalLayout() { //获取改变后的根布局 Rect rect = new Rect(); decordView.getWindowVisibleDisplayFrame(rect); int disHeight = rect.bottom - rect.top; // 比较Activity根布局与当前布局的大小 int heightDiff = decordView.getRootView().getHeight() - disHeight; if (mBtnLoginBottomHeight == -1) { //登录按钮距底部的高度 int position[] = new int[2]; moBtnLogin.getLocationOnScreen(position); mBtnLoginBottomHeight = FeizaoApp.metrics.heightPixels - position[1] - moBtnLogin.getHeight(); } //高度差大于100 且 大于登录按钮距底部的距离,滚动登录按钮的父View //不能直接滚动跟布局 以及 单独的View 详细请看scrollTo方法的介绍再次不做赘述 if (heightDiff > 100 && mBtnLoginBottomHeight <= heightDiff) ((View) moBtnLogin.getParent()).scrollTo(0, heightDiff - mBtnLoginBottomHeight + scaleHeight);//键盘显示操作 else ((View) moBtnLogin.getParent()).scrollTo(0, 0);//键盘隐藏操作 } }); }
布局文件代码:
<RelativeLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/login_bg2"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/title" android:layout_centerHorizontal="true" android:layout_marginBottom="43.33dp" android:layout_marginTop="91.33dp" android:contentDescription="@string/app_name" android:src="@drawable/login_logo" /> <RelativeLayout android:id="@+id/login_ll_account" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/logo" android:layout_centerHorizontal="true" android:layout_marginLeft="30dip" android:layout_marginRight="30dip" android:gravity="center" > <ImageView android:id="@+id/login_iv_account" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16.66dp" android:layout_marginTop="2dip" android:contentDescription="@string/app_name" android:src="@drawable/ic_login_account" /> <EditText android:id="@+id/login_et_account" android:layout_width="match_parent" android:layout_height="30dip" android:layout_toRightOf="@id/login_iv_account" android:background="@color/trans" android:gravity="top" android:hint="@string/input_username" android:maxLength="30" android:paddingLeft="10dp" android:paddingRight="10dp" android:singleLine="true" android:textColor="@color/a_text_color_ffffff" android:textColorHint="@color/edit_text_gray" android:textSize="@dimen/a_text_size_42" /> <View style="@style/divider_horizontal" android:layout_below="@id/login_et_account" /> <ImageView android:id="@+id/login_iv_account_delete" android:layout_width="30dp" android:layout_height="30dp" android:padding="6dp" android:src="@drawable/icon_delete_x" android:visibility="gone" /> </RelativeLayout> <RelativeLayout android:id="@+id/login_ll_pwd" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/login_ll_account" android:layout_centerHorizontal="true" android:layout_marginLeft="30dip" android:layout_marginRight="30dip" android:layout_marginTop="26.66dp" android:gravity="center" > <ImageView android:id="@+id/login_iv_psw" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16.66dp" android:layout_marginTop="2dip" android:contentDescription="@string/app_name" android:src="@drawable/ic_login_pwd" /> <EditText android:id="@+id/login_et_pwd" android:layout_width="match_parent" android:layout_height="30dip" android:layout_toRightOf="@id/login_iv_psw" android:background="@color/trans" android:gravity="top" android:hint="@string/input_password" android:inputType="textPassword" android:maxLength="30" android:paddingLeft="10dp" android:paddingRight="10dp" android:singleLine="true" android:textColor="@color/a_text_color_ffffff" android:textColorHint="@color/edit_text_gray" android:textSize="@dimen/a_text_size_42" /> <View style="@style/divider_horizontal" android:layout_below="@id/login_et_pwd" /> <ImageView android:id="@+id/login_iv_psw_delete" android:layout_width="30dp" android:layout_height="30dp" android:padding="6dp" android:src="@drawable/icon_delete_x" android:visibility="gone" /> </RelativeLayout> <Button android:id="@+id/login_btn_login" android:layout_width="match_parent" android:layout_height="42.66dp" android:layout_below="@id/login_ll_pwd" android:layout_centerHorizontal="true" android:layout_marginLeft="30dip" android:layout_marginRight="30dip" android:layout_marginTop="16.66dp" android:background="@drawable/btn_bg_send_selector" android:text="@string/login" android:textColor="@color/white" android:textSize="14sp" /> <LinearLayout android:id="@+id/other_operater" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/login_btn_login" android:layout_marginLeft="30dip" android:layout_marginRight="30dip" android:layout_marginTop="12dp" android:gravity="center_vertical" > <TextView android:id="@+id/login_btn_register" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:paddingBottom="2dip" android:paddingTop="2dip" android:text="@string/new_register" android:textColor="@color/a_text_ffffff_80ffffff_selector" android:textSize="@dimen/a_text_size_42" /> <View style="@style/divider_vertical" android:layout_height="14dp" android:layout_gravity="center_vertical" android:background="@color/a_text_color_ffffff" /> <TextView android:id="@+id/login_tv_forget_pwd" android:layout_width="0dip" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_horizontal" android:paddingBottom="2dip" android:paddingTop="2dip" android:text="@string/forget_password" android:textColor="@color/a_text_ffffff_80ffffff_selector" android:textSize="@dimen/a_text_size_42" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:gravity="center_horizontal" android:orientation="horizontal" > <ImageView android:id="@+id/login_ll_by_qq" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/app_name" android:src="@drawable/btn_qq_login_selector" /> <ImageView android:id="@+id/login_ll_by_weixin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="18dip" android:contentDescription="@string/app_name" android:src="@drawable/btn_weixin_login_selector" /> <ImageView android:id="@+id/login_ll_by_weibo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="18dip" android:layout_marginBottom="60dp" android:contentDescription="@string/app_name" android:src="@drawable/btn_weibo_login_selector" /> </LinearLayout> </RelativeLayout> </RelativeLayout>
再次提供demo链接,仅供参考:
下载Demo请点击
有问题请留言,或联系本人Qq:63724830
相关文章推荐
- android Json解析——揭开json解析的神秘面纱
- Android开发之SQLite的使用方法
- Android轮播循环ViewPager
- DNS 截持模拟及环境搭建
- androidstudio新建项目中在布局文件中不显示title的方法
- android 自定义进度条颜色
- android listview、GridView中item点击后改变其他item中的状态 setOnItemClickListener onItemClick
- Android中使用ON CONFLICT REPLACE同步数据到SQLITE
- Android 读取assets中的Json文件
- Android Support is disabled---Android Studio 2.0问题
- android回调简介
- Android Support兼容包详解<v4、v7>
- Android 让Retrofit与Realm、Parceler一起使用
- Android自动化测试之Monkey Test 安装(二)
- Android本地数据存储:Shared Preferences安全风险浅析
- Android Design Support Library--使用CoordinatorLayout来处理滚动
- android listview.onDraw中绘制的内容不在最顶层
- Android开发环境配置
- Android屏幕适配全攻略(最权威的官方适配指导)
- Android监听系统短信数据库变化-提取短信内容