本文主要是介绍解决Android编辑框在全屏模式下无法检测布局变化的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
解决Android编辑框在全屏模式下无法检测布局变化的问题
铺垫的知识请看我的另一篇博客:Android软键盘的显示和隐藏
- package com.jqbar;
- import android.content.Context;
- import android.util.AttributeSet;
- import android.util.Log;
- import android.widget.FrameLayout;
- public class MyFrameLayout extends FrameLayout{
- private onResizeListener listener;
- public interface onResizeListener
- {
- void OnResize(int w, int h, int oldw, int oldh);
- }
- public void setOnResizeListener(onResizeListener l) {
- listener = l;
- }
- public MyFrameLayout(Context context, AttributeSet attrs) {
- super(context, attrs);
- // TODO Auto-generated constructor stub
- }
- @Override
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
- // TODO Auto-generated method stub
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
- // Log.e("onMeasure ", "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec);
- }
- @Override
- protected void onLayout(boolean changed, int left, int top, int right,
- int bottom) {
- // TODO Auto-generated method stub
- super.onLayout(changed, left, top, right, bottom);
- Log.e("onLayout ", "=>OnLayout called! changed="+ changed+",l=" + left + ", t=" + top + ",r=" + right + ",b="+bottom);
- if(listener!=null)
- {
- listener.OnResize(left,top,right,bottom);
- }
- }
- @Override
- protected void onSizeChanged(int w, int h, int oldw, int oldh) {
- // TODO Auto-generated method stub
- super.onSizeChanged(w, h, oldw, oldh);
- // Log.e("onSizeChanged ", "=>onResize called! w="+w + ",h="+h+",oldw="+oldw+",oldh="+oldh);
- }
- }
package com.jqbar;import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.FrameLayout;public class MyFrameLayout extends FrameLayout{private onResizeListener listener;public interface onResizeListener{void OnResize(int w, int h, int oldw, int oldh);}public void setOnResizeListener(onResizeListener l) { listener = l;}public MyFrameLayout(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stub}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {// TODO Auto-generated method stubsuper.onMeasure(widthMeasureSpec, heightMeasureSpec);
// Log.e("onMeasure ", "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec); }@Overrideprotected void onLayout(boolean changed, int left, int top, int right,int bottom) {// TODO Auto-generated method stubsuper.onLayout(changed, left, top, right, bottom);Log.e("onLayout ", "=>OnLayout called! changed="+ changed+",l=" + left + ", t=" + top + ",r=" + right + ",b="+bottom); if(listener!=null){listener.OnResize(left,top,right,bottom);}}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {// TODO Auto-generated method stubsuper.onSizeChanged(w, h, oldw, oldh);
// Log.e("onSizeChanged ", "=>onResize called! w="+w + ",h="+h+",oldw="+oldw+",oldh="+oldh); }}
以上的代码在
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);设置全屏后,就算是布局改变了,但是onLayout函数里面相应的参数也不会改变。此时将无法通过检测参数的数值变化来监听软键盘的显示和隐藏,从而无法在显示和隐藏软键盘时实时改变自己的布局。
但是虽然传过来的参数是不对的,但是还是会调用相应的接口,所以通过相应的设置一些参数,并且根据我们程序的特点来限定各种状态,从而能够实现软键盘的显示和隐藏的监听。这就是我的总的思路。
- if(msg.what == MSG_HIDESOFTINPUT)
- {
- if(showSoftInput&&!bCallTextEdit)
- {
- mScrollView.scrollBy(0, 50-dy);
- showSoftInput = false ;
- }
- else if(showSoftInput&&bCallTextEdit)
- {
- callTimeCount += 1;
- if(callTimeCount == 2)
- {
- bCallTextEdit = false;
- callTimeCount = 0;
- }
- }
if(msg.what == MSG_HIDESOFTINPUT){if(showSoftInput&&!bCallTextEdit){mScrollView.scrollBy(0, 50-dy);showSoftInput = false ;}else if(showSoftInput&&bCallTextEdit){callTimeCount += 1;if(callTimeCount == 2){bCallTextEdit = false;callTimeCount = 0;}}
其中的showSoftInput 和bCallTextEdit为boolean类型
这里的想法是: 当弹出软键盘时showSoftInput和bCallTextEdit都为true,此时我们修改我们的布局,并且弹出软键盘时会监听到布局变化,会执行
callTimeCount += 1;
if(callTimeCount == 2)
{
bCallTextEdit = false;
callTimeCount = 0;
}
这些语句,此时onLayout会被调用两次,所以用callTimeCount来记数,然后设置bCallTextEdit 为false,这时的状态是软键盘为显示状态。
点击隐藏软键盘时会执行这些语句
if(showSoftInput&&!bCallTextEdit)
{
mScrollView.scrollBy(0, 50-dy);//这里我们就可以恢复我们的布局了
showSoftInput = false ;
}
整体的思路就是这样,我们不能通过onLayout参数的改变来监听我们的布局改变,那么我们自己来设置参数来控制相应的状态,从而实现实时监听。具体的细节就不再叙述了,感觉写的有点乱。
这篇关于解决Android编辑框在全屏模式下无法检测布局变化的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!