版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】如何解决android软键盘挡住输入框方法
方法一:非透明状态栏下使用adjustResize和adjustPan,或是透明状态栏下使用fitsSystemWindows=true属性/upload/information/20200623/125/123905.png/upload/information/20200623/125/123906.png1、adjustPan/upload/information/20200623/125/123907.png2、adjustResize/upload/information/20200623/125/123908.png方法二:在界面最外层布局包裹ScrollView/upload/information/20200623/125/123909.png/upload/information/20200623/125/123910.png
<activity
android:name=".TestInputActivity"
android:windowSoftInputMode="adjustPan">/upload/information/20200623/125/123911.png/upload/information/20200623/125/123912.png
<activity
android:name=".TestInputActivity"
android:windowSoftInputMode="adjustResize">/upload/information/20200623/125/123913.png/upload/information/20200623/125/123914.png沉浸式状态栏/透明状态栏情况下方法三:使用scrollTo方法,当键盘弹起时,让界面整体上移;键盘收起,让界面整体下移<RelativeLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
tools:context="com.example.liubin1.softkeyboardhelper.MainActivity">
<EditText
android:id="@+id/name"
android:hint="请输入用户名:"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="50dp"
/>
<EditText
android:id="@+id/pas"
android:layout_below="@id/name"
android:hint="请输入密码:"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="50dp"
/>
<Button
android:id="@+id/login_btn"
android:layout_below="@id/rpas"
android:layout_centerHorizontal="true"
android:text="登录"
android:layout_width="180dp"
android:layout_height="50dp"
/>
</RelativeLayout>RelativeLayout
main
=
(RelativeLayout)
findViewById(R.id.main);Button
login_btn
=
(Button)
findViewById(R.id.login_btn);//在Activity的onCreate里添加如下方法addLayoutListener(main,login_btn);/**
*
addLayoutListener方法如下
*
@param
main
根布局
*
@param
scroll
需要显示的最下方View
*/
public
void
addLayoutListener(final
View
main,
final
View
scroll)
{
main.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener()
{
@Override
public
void
onGlobalLayout()
{
Rect
rect
=
new
Rect();
//1、获取main在窗体的可视区域
main.getWindowVisibleDisplayFrame(rect);
//2、获取main在窗体的不可视区域高度,在键盘没有弹起时,main.getRootView().getHeight()调节度应该和rect.bottom高度一样
int
mainInvisibleHeight
=
main.getRootView().getHeight()
-
rect.bottom;
int
screenHeight
=
main.getRootView().getHeight();//屏幕高度
//3、不可见区域大于屏幕本身高度的1/4:说明键盘弹起了
if
(mainInvisibleHeight
>
screenHeight
/
4)
{
int[]
location
=
new
int[2];
scroll.getLocationInWindow(location);
//
4、获取Scroll的窗体坐标,算出main需要滚动的高度
int
srollHeight
=
(location[1]
+
scroll.getHeight())
-
rect.bottom;
//5、让界面整体上移键盘的高度
main.scrollTo(0,
srollHeight);
}
else
{
//3、不可见区域小于屏幕高度1/4时,说明键盘隐藏了,把界面下移,移回到原有高度
main.scrollTo(0,
0);
}
}
});
}
}/upload/information/20200623/125/123915.gif方法四:适配键盘高度变化情况,当键盘弹起时,让界面整体上移;键盘收起,让界面整体下移public
class
KeyboardLayout
extends
FrameLayout
{
private
KeyboardLayoutListener
mListener;
private
boolean
mIsKeyboardActive
=
false;
//输入法是否激活
private
int
mKeyboardHeight
=
0;
//
输入法高度
public
KeyboardLayout(Context
context)
{
this(context,
null,
0);
}
public
KeyboardLayout(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
0);
}
public
KeyboardLayout(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
//
监听布局变化
getViewTreeObserver().addOnGlobalLayoutListener(new
KeyboardOnGlobalChangeListener());
}
public
void
setKeyboardListener(KeyboardLayoutListener
listener)
{
mListener
=
listener;
}
public
KeyboardLayoutListener
getKeyboardListener()
{
return
mListener;
}
public
boolean
isKeyboardActive()
{
return
mIsKeyboardActive;
}
/**
*
获取输入法高度
*
*
@return
*/
public
int
getKeyboardHeight()
{
return
mKeyboardHeight;
}
public
interface
KeyboardLayoutListener
{
/**
*
@param
isActive
输入法是否激活
*
@param
keyboardHeight
输入法面板高度
*/
void
onKeyboardStateChanged(boolean
isActive,
int
keyboardHeight);
}
private
class
KeyboardOnGlobalChangeListener
implements
ViewTreeObserver.OnGlobalLayoutListener
{
int
mScreenHeight
=
0;
private
int
getScreenHeight()
{
if
(mScreenHeight
>
0)
{
return
mScreenHeight;
}
mScreenHeight
=
((WindowManager)
getContext().getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getHeight();
return
mScreenHeight;
}
@Override
public
void
onGlobalLayout()
{
Rect
rect
=
new
Rect();
//
获取当前页面窗口的显示范围
((Activity)
getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int
screenHeight
=
getScreenHeight();
int
keyboardHeight
=
screenHeight
-
rect.bottom;
//
输入法的高度
boolean
isActive
=
false;
if
(Math.abs(keyboardHeight)
>
screenHeight
/
4)
{
isActive
=
true;
//
超过屏幕五分之一则表示弹出了输入法
mKeyboardHeight
=
keyboardHeight;
}
mIsKeyboardActive
=
isActive;
if
(mListener
!=
null)
{
mListener.onKeyboardStateChanged(isActive,
keyboardHeight);
}
}
}
}public
class
KeyboardLayout
extends
FrameLayout
{
private
KeyboardLayoutListener
mListener;
private
boolean
mIsKeyboardActive
=
false;
//输入法是否激活
private
int
mKeyboardHeight
=
0;
//
输入法高度
public
KeyboardLayout(Context
context)
{
this(context,
null,
0);
}
public
KeyboardLayout(Context
context,
AttributeSet
attrs)
{
this(context,
attrs,
0);
}
public
KeyboardLayout(Context
context,
AttributeSet
attrs,
int
defStyleAttr)
{
super(context,
attrs,
defStyleAttr);
//
监听布局变化
getViewTreeObserver().addOnGlobalLayoutListener(new
KeyboardOnGlobalChangeListener());
}
public
void
setKeyboardListener(KeyboardLayoutListener
listener)
{
mListener
=
listener;
}
public
KeyboardLayoutListener
getKeyboardListener()
{
return
mListener;
}
public
boolean
isKeyboardActive()
{
return
mIsKeyboardActive;
}
/**
*
获取输入法高度
*
*
@return
*/
public
int
getKeyboardHeight()
{
return
mKeyboardHeight;
}
public
interface
KeyboardLayoutListener
{
/**
*
@param
isActive
输入法是否激活
*
@param
keyboardHeight
输入法面板高度
*/
void
onKeyboardStateChanged(boolean
isActive,
int
keyboardHeight);
}
private
class
KeyboardOnGlobalChangeListener
implements
ViewTreeObserver.OnGlobalLayoutListener
{
int
mScreenHeight
=
0;
private
int
getScreenHeight()
{
if
(mScreenHeight
>
0)
{
return
mScreenHeight;
}
mScreenHeight
=
((WindowManager)
getContext().getSystemService(Context.WINDOW_SERVICE))
.getDefaultDisplay().getHeight();
return
mScreenHeight;
}
@Override
public
void
onGlobalLayout()
{
Rect
rect
=
new
Rect();
//
获取当前页面窗口的显示范围
((Activity)
getContext()).getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int
screenHeight
=
getScreenHeight();
int
keyboardHeight
=
screenHeight
-
rect.bottom;
//
输入法的高度
boolean
isActive
=
false;
if
(Math.abs(keyboardHeight)
>
screenHeight
/
4)
{
isActive
=
true;
//
超过屏幕五分之一则表示弹出了输入法
mKeyboardHeight
=
keyboardHeight;
}
mIsKeyboardActive
=
isActive;
if
(mListener
!=
null)
{
mListener.onKeyboardStateChanged(isActive,
keyboardHeight);
}
}
}
}
/**
*
监听键盘状态,布局有变化时,靠scrollView去滚动界面
*/
public
void
addLayoutListener()
{
bindingView.mainLl.setKeyboardListener(new
KeyboardLayout.KeyboardLayoutListener()
{
@Override
public
void
onKeyboardStateChanged(boolean
isActive,
int
keyboardHeight)
{
Log.e("onKeyboardStateChanged",
"isActive:"
+
isActive
+
"
keyboardHeight:"
+
keyboardHeight);
if
(isActive)
{
scrollToBottom();
}
}
});
}
/**
*
弹出软键盘时将SVContainer滑到底
*/
private
void
scrollToBottom()
{
bindingView.loginLl.postDelayed(new
Runnable()
{
@Override
public
void
run()
{
bindingView.loginLl.smoothScrollTo(0,
bindingView.loginLl.getBottom()
+
SoftKeyInputHidWidget.getStatusBarHeight(LoginActivityForDiffkeyboardHeight.this));
}
},
100);
}/upload/information/20200623/125/123916.gif方法五:监听Activity顶层View,判断软键盘是否弹起,对界面重新绘制/**
*
解决键盘档住输入框
*
Created
by
SmileXie
on
2017/4/3.
*/public
class
SoftHideKeyBoardUtil
{
public
static
void
assistActivity
(Activity
activity)
{
new
SoftHideKeyBoardUtil(activity);
}
private
View
mChildOfContent;
private
int
usableHeightPrevious;
private
FrameLayout.LayoutParams
frameLayoutParams;
//为适应华为小米等手机键盘上方出现黑条或不适配
private
int
contentHeight;//获取setContentView本来view的高度
private
boolean
isfirst
=
true;//只用获取一次
private
int
statusBarHeight;//状态栏高度
private
SoftHideKeyBoardUtil(Activity
activity)
{
//1、找到Activity的最外层布局控件,它其实是一个DecorView,它所用的控件就是FrameLayout
FrameLayout
content
=
(FrameLayout)
activity.findViewById(android.R.id.content);
//2、获取到setContentView放进去的View
mChildOfContent
=
content.getChildAt(0);
//3、给Activity的xml布局设置View树监听,当布局有变化,如键盘弹出或收起时,都会回调此监听
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new
ViewTreeObserver.OnGlobalLayoutListener()
{
//4、软键盘弹起会使GlobalLayout发生变化
public
void
onGlobalLayout()
{
if
(isfirst)
{
contentHeight
=
mChildOfContent.getHeight();//兼容华为等机型
isfirst
=
false;
}
//5、当前布局发生变化时,对Activity的xml布局进行重绘
possiblyResizeChildOfContent();
}
});
//6、获取到Activity的xml布局的放置参数
frameLayoutParams
=
(FrameLayout.LayoutParams)
mChildOfContent.getLayoutParams();
}
//
获取界面可用高度,如果软键盘弹起后,Activity的xml布局可用高度需要减去键盘高度
private
void
possiblyResizeChildOfContent()
{
//1、获取当前界面可用高度,键盘弹起后,当前界面可用布局会减少键盘的高度
int
usableHeightNow
=
computeUsableHeight();
//2、如果当前可用高度和原始值不一样
if
(usableHeightNow
!=
usableHeightPrevious)
{
//3、获取Activity中xml中布局在当前界面显示的高度
int
usableHeightSansKeyboard
=
mChildOfContent.getRootView().getHeight();
//4、Activity中xml布局的高度-当前可用高度
int
heightDifference
=
usableHeightSansKeyboard
-
usableHeightNow;
//5、高度差大于屏幕1/4时,说明键盘弹出
if
(heightDifference
>
(usableHeightSansKeyboard/4))
{
//
6、键盘弹出了,Activity的xml布局高度应当减去键盘高度
if
(Bu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 动物用防寄生虫颈圈项目评价分析报告
- 电视机解码器产业运行及前景预测报告
- 洗碗机用清洁剂产业深度调研及未来发展现状趋势
- 照明装置产业运行及前景预测报告
- 梳子盒市场发展现状调查及供需格局分析预测报告
- 《液压挖掘机培训》课件
- 《生态文明主题班会》课件
- 牙科用烘箱产品入市调查研究报告
- 液体浴皂市场发展预测和趋势分析
- 2024年度产品推广合同
- 全国高职高专英语写作大赛
- 微机原理与接口技术8259A练习题及答案
- 幼儿园小朋友认识医生和护士(课堂PPT)
- 汽车总线测试方案概要
- 商铺装修工程施工方案.
- 形式发票样本(Proforma Invoice)
- 草坪铺设施工方案
- 临床路径实施情况、存在问题及整改措施
- (完整word版)上海博物馆文物术语中英文对照
- 调度自动化及通信技术监督实施细则
- 学、练、评一体化课堂模式下赛的两个问题与对策
评论
0/150
提交评论