安卓学习笔记---解决在在Edittext输入的时候,输入框被软键盘遮挡部分内容的问题(一)
来源:互联网 发布:linux 列出网卡ip 编辑:程序博客网 时间:2024/06/05 12:45
这几天做项目的时候发现了一个问题,就是在做登录界面的时候,在点击密码输入框输入密码的时候,总是会出现这种情况:
首先是最开始的界面:
然后我点击密码输入框的时候,软键盘弹出的界面是这样的额,遮挡住了部分输入框:
这显然不是我们想要的效果,我想要的是将密码输入框以上的控件都上移,不被覆盖,需求知道了,现在就是怎么解决了,首先不管是问的群里的一些人,他们说在Mainfest清单文件里面的Activity设置:
<activity android:name=".user.LoginActivity" android:screenOrientation="portrait" android:windowSoftInputMode=
"stateVisible|adjustResize"
屏幕整体上移
/>
我添加进去了,但是却没什么效果,不符合要求,后来有查到说是全屏的这样设置是不不能成功的,好吧,又开始百度其他的方法,
有说这样可以:
网址:http://blog.csdn.net/xiechengfa/article/details/51122823
具体内容是这样的:
全屏模式下,即使将activity的windowSoftInputMode的属性设置为:adjustResize,在键盘显示时它未将Activity的Screen向上推动,所以你Activity的view的根树的尺寸是没有变化的。在这种情况下,你也就无法得知键盘的尺寸,对根view的作相应的推移。全屏下的键盘无法Resize的问题从2.1就已经存在了,直到现在google还未给予解决。感谢Ricardo提供的轮子,他在stackoverflow找到了解决方案。有人已经封装好了该类,你只需引用就OK了。使用方法在你的Activity的oncreate()方法里调用AndroidBug5497Workaround.assistActivity(this);即可。注意:在setContentView(R.layout.xxx)之后调用。
OK,找到方法了,就开始测试了,我添加进去之后,but,依然不能成功,真不知道怎么办好了,快2天了!,然后继续百度,终于看到有说的这么一个方法:
网址:
http://blog.csdn.net/wk843620202/article/details/51580113
基本内容是这样的:
安卓界面虚拟键盘弹出下面布局怎么顶上去,解决方法如下:
1,在布局文件中,设置ScrollView :
<ScrollView
android:layout_width="fill_parent"android:layout_height="fill_parent"
android:fillViewport="true"
android:orientation="vertical"
android:scrollbars="vertical">
<EditText
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</ScrollView>
2,在activity的清单文件中,设置:
<activity
android:name=".PaiCheActivity"
android:label="@string/title_activity_pai_che"
android:windowSoftInputMode="stateHidden"/>
当ScrollView里的元素想填满ScrollView时,使用"fill_parent"是不管用的,必需为ScrollView设置:android:fillViewport="true"。
stateHidden:用户选择activity时,软键盘总是被隐藏
这样做布局的底部会随着软键盘的弹出而上移。
我是这么做的:
Login.xml界面:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/login_bg"> <include layout="@layout/title_bar"></include> <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:fillViewport="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linear_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_marginTop="@dimen/dimen48" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/img_login" /> </LinearLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/rl_account" android:layout_marginLeft="@dimen/dimen30" android:layout_marginRight="@dimen/dimen30" android:layout_width="match_parent" android:layout_height="@dimen/dimen50" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/dimen30" > <TextView android:id="@+id/tv_zhanghao" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="账号" android:textColor="@color/a0081bf" android:layout_marginRight="@dimen/dimen15" android:textSize="@dimen/size15" /> <com.hxlm.ekela.android.view.ContainsEmojiEditText android:id="@+id/et_account" android:layout_toRightOf="@+id/tv_zhanghao" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:background="@null" android:hint="请输入手机号" android:inputType="number" android:maxLength="11" android:singleLine="true" android:textColor="@color/shenhuise" android:textColorHint="@color/ziticolor" android:textSize="@dimen/size15" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/a3dbddf" android:layout_alignParentBottom="true" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_password" android:layout_width="match_parent" android:layout_height="@dimen/dimen50" android:layout_marginLeft="@dimen/dimen30" android:layout_marginRight="@dimen/dimen30" android:layout_below="@id/rl_account" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/dimen10" > <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="密码" android:textColor="@color/a0081bf" android:layout_marginRight="@dimen/dimen15" android:textSize="@dimen/size15" /> <com.hxlm.ekela.android.view.ContainsEmojiEditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_toRightOf="@id/tv_password" android:background="@null" android:hint="请输入密码" android:inputType="textPassword" android:singleLine="true" android:textColor="@color/shenhuise" android:textColorHint="@color/ziticolor" android:textSize="@dimen/size15" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/a3dbddf" android:layout_alignParentBottom="true" /> </RelativeLayout> <Button android:id="@+id/bt_login" android:layout_marginLeft="@dimen/dimen25" android:layout_marginRight="@dimen/dimen25" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@+id/rl_password" android:layout_marginTop="@dimen/dimen32" android:background="@drawable/login_submit" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/bt_login" android:layout_marginTop="@dimen/dimen30" android:layout_marginLeft="@dimen/dimen25" android:layout_marginRight="@dimen/dimen25" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:orientation="vertical" > <TextView android:id="@+id/tv_zhuce" android:layout_width="@dimen/dimen116" android:layout_height="wrap_content" android:textSize="@dimen/size12" android:textColor="@color/a0081bf" android:text="@string/dialog_forget_resist" android:gravity="center" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:orientation="vertical" > <TextView android:id="@+id/tv_forget_password" android:layout_width="@dimen/dimen76" android:layout_height="wrap_content" android:textSize="@dimen/size12" android:gravity="center" android:textColor="@color/a9fa0a0" android:text="@string/dialog_forget_password"/> </LinearLayout> </RelativeLayout> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/login_bottom_bg" android:layout_alignParentBottom="true" /> </RelativeLayout> </LinearLayout> </ScrollView></LinearLayout>
突出的是重要的显示,然后就是在Mainfest.xml清单文件里面LoginActivity进行设置:<activity android:name=".user.LoginActivity" android:screenOrientation="portrait" android:windowSoftInputMode="adjustResize|stateHidden"/>
然后运行,实现的效果是这样的:
这样感觉就好了,比前面的好多了,不知道是什么原因,使用的其他方法就是不能实现呢。
我最近再看,还有一种实现方法:
转载网址:
http://blog.csdn.net/qq_23003305/article/details/49763731
内容是这样写的:
一、概述
—–我们在开发中可能会遇到当我们的输入框EditText获取到焦点后,软件盘弹出遮挡了我们的一些控价。这种情况尤其带注册和登录的界面比较常见。虽然这不是什么大问题,但它却是很影响我们的用户体验。本文就是来解决这个小题目。
二、需解决两个问题
- 1、当第一次进入界面是,EditText控件会获取到焦点,弹出软件盘
——–解决办法:在activity中的onCreate()方法中加入以下代码:
—–该方法的调用放在setContentView()方法之前。这句代码的意思是设置软件盘隐藏并且在弹出软件盘后能自适应屏幕。
- 2、在用到上述的设置后,弹出的软件盘会遮挡EditText下的第一个控件(本例是第一个Button)以下的部分,这里不加margin属性。
——在我们知道了问题后,解决问题也就有了方向。经过测试,得出结论,当软件盘弹出后,我们布局变化的原因是因为屏幕发生了上移,然而我们最外层的布局就是依赖屏幕的尺寸而创建的(高度设置为match-parent)。换言之,我们只要操作最外层的layout布局就可以了。当其高度大小发生改变时,给我们一个回调,我们根据其变化就知道是否弹出软件盘,当弹出软件盘时,我们给它设置padding-top为一定的负值,这个值就是遮挡的高度,肯定是可以得到的。当软件盘消失时,将这个值置零。我们的问题解决思路就是这个样子。
—–接下来就是自定义我们最外层布局的时候了,就叫他SoftInputJustLayout吧(这个随便你了),别说话,看代码:
—– 可能会有细心的朋友会问为什么onSizeChange()方法中设置的pading-top为-btn的两倍,因为我最后一个EditText下面有3个button要显示,而软件盘弹出时会遮挡EditText下面的一个(为啥是一个,原因我也不知道)。当然如果你设置了e最后一个EditText以下的控价的margin值,那也必须的都得加上才不会使有些控件被遮挡。
——正是因为这个padding-top值的计算有些麻烦,那么我们就必须找到更好的解决办法,很简单的多哦,只需在我们的activity_main.xml文件中的id=ll_main_login的布局外加上ScrollView,找到该控件,然后在onSizeChange()方法里面做处理(具体代码自己参考如下写吧,肯定没错的。
最后实现的效果图
————-
看起来也是不错的样子,然后我下载demo,按照自己的需求改了改,发现也是能实现这种效果:首先是Login2.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="@dimen/dimen50" android:background="@drawable/title_bar_bg" > <LinearLayout android:id="@+id/linear_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:gravity="center_vertical" android:orientation="horizontal" android:padding="@dimen/dimen12" > <ImageView android:layout_width="wrap_content" android:layout_height="@dimen/dimen24" android:src="@drawable/setting_back" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dimen5" android:text="返回" android:textColor="@color/white" android:textSize="@dimen/size15" android:visibility="gone" /> </LinearLayout> <ImageView android:id="@+id/iv_backtuwen" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_centerVertical="true" android:paddingLeft="@dimen/dimen24" android:paddingRight="@dimen/dimen24" android:src="@drawable/setting_back" android:visibility="gone" /> <TextView android:id="@+id/tv_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_centerVertical="true" android:textColor="@color/white" android:textSize="@dimen/size16" android:text="意克拉登录" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:gravity="center_vertical" android:orientation="horizontal" > <ImageView android:id="@+id/iv_callcustomer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="@dimen/dimen12" android:src="@drawable/title_bar_rightofleft" android:visibility="gone" /> </LinearLayout> </RelativeLayout><com.keyboard.view.SoftInputJustLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/login_bg" android:orientation="vertical" android:id="@+id/softinputlyout_main"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="none" android:fillViewport="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:id="@+id/linear_top" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center" android:layout_marginTop="@dimen/dimen48" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/img_login" /> </LinearLayout> <RelativeLayout android:id="@+id/realtive_pass" android:layout_width="match_parent" android:layout_height="match_parent" > <RelativeLayout android:id="@+id/rl_account" android:layout_marginLeft="@dimen/dimen30" android:layout_marginRight="@dimen/dimen30" android:layout_width="match_parent" android:layout_height="@dimen/dimen50" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/dimen30" > <TextView android:id="@+id/tv_zhanghao" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="账号" android:textColor="@color/a0081bf" android:layout_marginRight="@dimen/dimen15" android:textSize="@dimen/size15" /> <com.keyboard.view.ContainsEmojiEditText android:id="@+id/et_account" android:layout_toRightOf="@+id/tv_zhanghao" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:background="@null" android:hint="请输入手机号" android:inputType="number" android:maxLength="11" android:singleLine="true" android:textColor="@color/shenhuise" android:textColorHint="@color/ziticolor" android:textSize="@dimen/size15" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/a3dbddf" android:layout_alignParentBottom="true" /> </RelativeLayout> <RelativeLayout android:id="@+id/rl_password" android:layout_width="match_parent" android:layout_height="@dimen/dimen50" android:layout_marginLeft="@dimen/dimen30" android:layout_marginRight="@dimen/dimen30" android:layout_below="@id/rl_account" android:layout_centerHorizontal="true" android:layout_marginTop="@dimen/dimen10" > <TextView android:id="@+id/tv_password" android:layout_width="wrap_content" android:layout_height="match_parent" android:gravity="center_vertical" android:text="密码" android:textColor="@color/a0081bf" android:layout_marginRight="@dimen/dimen15" android:textSize="@dimen/size15" /> <com.keyboard.view.ContainsEmojiEditText android:id="@+id/et_password" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerVertical="true" android:layout_toRightOf="@id/tv_password" android:background="@null" android:hint="请输入密码" android:inputType="textPassword" android:singleLine="true" android:textColor="@color/shenhuise" android:textColorHint="@color/ziticolor" android:textSize="@dimen/size15" /> <View android:layout_width="match_parent" android:layout_height="1dp" android:background="@color/a3dbddf" android:layout_alignParentBottom="true" /> </RelativeLayout> <Button android:id="@+id/bt_login" android:layout_marginLeft="@dimen/dimen25" android:layout_marginRight="@dimen/dimen25" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_below="@+id/rl_password" android:layout_marginTop="@dimen/dimen32" android:background="@drawable/login_submit" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@+id/bt_login" android:layout_marginTop="@dimen/dimen30" android:layout_marginLeft="@dimen/dimen25" android:layout_marginRight="@dimen/dimen25" > <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:orientation="vertical" > <TextView android:id="@+id/tv_zhuce" android:layout_width="@dimen/dimen116" android:layout_height="wrap_content" android:textSize="@dimen/size12" android:textColor="@color/a0081bf" android:text="注册" android:gravity="center" /> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:orientation="vertical" > <TextView android:id="@+id/tv_forget_password" android:layout_width="@dimen/dimen76" android:layout_height="wrap_content" android:textSize="@dimen/size12" android:gravity="center" android:textColor="@color/a9fa0a0" android:text="忘记密码"/> </LinearLayout> </RelativeLayout> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/login_bottom_bg" android:layout_alignParentBottom="true" /> </RelativeLayout> </LinearLayout> </ScrollView></com.keyboard.view.SoftInputJustLayout></LinearLayout>然后就是LoginActivity2.java
public class LoginActivity4 extends Activity implements SoftInputJustLayout.OnSizeChangedListener { SoftInputJustLayout sl; private Button btn; private ScrollView scrollView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN | WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); setContentView(R.layout.dialog_login4); initView(); sl.setOnSizeChangedListener(this); } private void initView() { sl = (SoftInputJustLayout) findViewById(R.id.softinputlyout_main); btn = (Button) findViewById(R.id.bt_login); scrollView=(ScrollView)findViewById(R.id.scrollView); } @Override public void onSizeChange(boolean isChanged) { if (isChanged) { scrollView.scrollTo(0,0); // sl.setPadding(0, -btn.getMeasuredHeight() * 2, 0, 0);//最好的做法是用scrollview将字 view包起来,然后当isChanged=true时滑动到底部 } else { sl.setPadding(0, 0, 0, 0); } }}OK,完成,这是我觉得这两种方式都能实现的方式了,但都是在Activity适用,如果是一个Dialog形式的,就不起作用了,不知道为什么,哪位大神知道,可以告诉啊。
下一篇我就要收集一些比较常用的说的解决这个软键盘遮挡的问题了
- 安卓学习笔记---解决在在Edittext输入的时候,输入框被软键盘遮挡部分内容的问题(一)
- 安卓学习笔记---解决在在Edittext输入的时候,输入框被软键盘遮挡部分内容的问题(二)
- 解决popupwindow中有输入框的时候,弹出软键盘遮挡布局的问题
- 解决安卓软键盘遮挡输入框
- Android 解决在页面底部置输入框,软键盘遮挡部分输入框的问题
- Android解决被软键盘遮挡的EditText问题
- JS 解决安卓手机输入框被软键盘遮住的问题
- Android WebView 软键盘遮挡输入框问题的解决方法
- 当用软键盘输入时输入框被键盘遮挡的问题
- ios解决软键盘遮挡输入框问题
- js解决软键盘遮挡输入框问题
- IOS中输入框被软键盘遮挡的解决办法
- IOS中输入框被软键盘遮挡的解决办法
- IOS中输入框被软键盘遮挡的解决办法
- IOS中输入框被软键盘遮挡的解决办法
- IOS中输入框被软键盘遮挡的解决办法
- IOS中输入框被软键盘遮挡的解决办法
- IOS中输入框被软键盘遮挡的解决办法
- NSURLSession
- mac 使用 Homebrew 安装 mysql
- Windows 10 安装 Chocolatey
- javascript 引用类型 - Array类型
- Ruby gem: Mac 系统下的安装与更新
- 安卓学习笔记---解决在在Edittext输入的时候,输入框被软键盘遮挡部分内容的问题(一)
- 打开系统的设置页面
- matlab的函数conv()的C语言实现
- 插入排序
- Java内存溢出详解
- 【CodeForces】[659A]New Year and Days
- fitnesse使用
- scsi设备扫描特征分析
- 关于iOS通知(APNS)的理解