在做Android程序时,遇到这么一种情况。
当软键盘不显示时,底部布局如下。
其中红框是一个编辑框EditText,下同。
当软键盘弹出后,底部布局如下。
整体布局文件如下(只显示相关部分)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/all_rell"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<!--中间部分省略-->
<!--底部信息框-->
<RelativeLayout
android:id="@+id/bottom_rel"
android:layout_width="match_parent"
android:layout_height="@dimen/dimen_40_dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="@color/gray"
android:gravity="center_vertical">
<ImageView
android:id="@+id/exit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/dimen_5_dp"
android:layout_marginRight="@dimen/dimen_5_dp"
android:background="@drawable/write_comment"
android:padding="@dimen/dimen_12_dp" />
<ImageButton
android:id="@+id/botom_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/write_3circle" />
<ImageButton
android:id="@+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@id/botom_more"
android:padding="@dimen/dimen_5_dp"
android:src="@drawable/share" />
<TextView
android:id="@+id/commnet_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/dimen_5_dp"
android:layout_toLeftOf="@id/share"
android:drawableLeft="@drawable/comment"
android:drawablePadding="@dimen/dimen_10_dp"
android:text="214" />
<TextView
android:id="@+id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="@dimen/dimen_10_dp"
android:text="发送"
android:visibility="gone" />
<EditText
android:id="@+id/comment_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toLeftOf="@id/commnet_number"
android:layout_toRightOf="@id/exit_image"
android:background="@null"
android:hint="写评论" />
</RelativeLayout>
</RelativeLayout>
现在的需求为,监听软键盘的状态,当软键盘状态改变时,布局也会改变。为了达到效果,则可以通过使Activity实现View.OnLayoutChangeListener这个接口来实现。
首先,必须使Activity实现View.OnLayoutChangeListener接口,并且实现onLayoutChange这个方法,代码如下
public class NewsActivity extends BaseActivity implements View.OnLayoutChangeListener{
/*
根布局改变监听事件
*/
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
}
}
其中参数含义如下:
int left,int top,int right,int bottom为软键盘改变后的窗体可见部分的坐标,
int oldLeft,int oldTop,int oldRight,int oldBottom为软键盘改变前的窗体可见部分的坐标。
假定软键盘弹出后所占的高度为屏幕高度的1/3,那么,根据Android中的坐标体系可知,当(oldBottom-bottom)>屏幕高度的1/3时(原来的屏幕高度比现在的屏幕高度要高),我们就可以认为,此时软键盘是显示的;而当(bottom-oldBottom)>屏幕高度的1/3时(现在的屏幕高度比原来的屏幕高度要高),我们就可以认为,此时软键盘是隐藏的。
所以,我们实现功能的代码如下:
/*
根布局改变监听事件
*/
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//假定软键盘弹出后所占高度为屏幕高度的1/3
if(oldBottom != 0 && bottom != 0 && (oldBottom - bottom > ScreenUtils.getScreenHeight(this)/3)){
//软键盘显示
commnetNumber.setVisibility(View.GONE);
botomMore.setVisibility(View.GONE);
share.setVisibility(View.GONE);
send.setVisibility(View.VISIBLE);
//编辑框长度
RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) commentText.getLayoutParams();
params.setMargins(0,0, DensityUtils.dp2px(this,50),0);
commentText.setLayoutParams(params);
}else if(oldBottom != 0 && bottom != 0 && (bottom - oldBottom > ScreenUtils.getScreenHeight(this)/3)){
//软键盘隐藏
commnetNumber.setVisibility(View.VISIBLE);
botomMore.setVisibility(View.VISIBLE);
share.setVisibility(View.VISIBLE);
send.setVisibility(View.GONE);
//编辑框长度
RelativeLayout.LayoutParams params= (RelativeLayout.LayoutParams) commentText.getLayoutParams();
params.setMargins(0,0, 0,0);
commentText.setLayoutParams(params);
}
}
然后给根布局添加状态改变监听事件就可以了
@Override
protected void onResume() {
super.onResume();
//给根布局添加状态监听器
allRel.addOnLayoutChangeListener(this);
}
这样,在软键盘显示状态改变时,我们就可以动态改变布局了!