解决Android编辑框在全屏模式下无法检测布局变化的问题

解决Android编辑框在全屏模式下无法检测布局变化的问题       
        分类:            Android 225人阅读 评论(0) 收藏 举报


铺垫的知识请看我的另一篇博客:Android软键盘的显示和隐藏

  1. package com.jqbar; 
  2.  
  3. import android.content.Context; 
  4. import android.util.AttributeSet; 
  5. import android.util.Log; 
  6. import android.widget.FrameLayout; 
  7.  
  8. public class MyFrameLayout extends FrameLayout{ 
  9.      
  10.     private onResizeListener listener; 
  11.      
  12.     public interface onResizeListener 
  13.     { 
  14.          void OnResize(int w, int h, int oldw, int oldh); 
  15.     } 
  16.      
  17.      public void setOnResizeListener(onResizeListener l) {  
  18.          listener = l; 
  19.      } 
  20.      
  21.     public MyFrameLayout(Context context, AttributeSet attrs) { 
  22.         super(context, attrs); 
  23.         // TODO Auto-generated constructor stub 
  24.     } 
  25.     @Override 
  26.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
  27.         // TODO Auto-generated method stub 
  28.         super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
  29. //      Log.e("onMeasure ", "=>onMeasure called! widthMeasureSpec=" + widthMeasureSpec + ", heightMeasureSpec=" + heightMeasureSpec);  
  30.     } 
  31.     @Override 
  32.     protected void onLayout(boolean changed, int left, int top, int right, 
  33.             int bottom) { 
  34.         // TODO Auto-generated method stub 
  35.         super.onLayout(changed, left, top, right, bottom); 
  36.         Log.e("onLayout ", "=>OnLayout called! changed="+ changed+",l=" + left + ", t=" + top + ",r=" + right + ",b="+bottom);    
  37.         if(listener!=null
  38.         { 
  39.             listener.OnResize(left,top,right,bottom); 
  40.         } 
  41.     } 
  42.     @Override 
  43.     protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
  44.         // TODO Auto-generated method stub 
  45.         super.onSizeChanged(w, h, oldw, oldh); 
  46. //       Log.e("onSizeChanged ", "=>onResize called! w="+w + ",h="+h+",oldw="+oldw+",oldh="+oldh);    
  47.     } 
  48.  
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);   
	}

}

以上的代码在

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);设置全屏后,就算是布局改变了,但是onLayout函数里面相应的参数也不会改变。此时将无法通过检测参数的数值变化来监听软键盘的显示和隐藏,从而无法在显示和隐藏软键盘时实时改变自己的布局。


但是虽然传过来的参数是不对的,但是还是会调用相应的接口,所以通过相应的设置一些参数,并且根据我们程序的特点来限定各种状态,从而能够实现软键盘的显示和隐藏的监听。这就是我的总的思路

  1. if(msg.what == MSG_HIDESOFTINPUT) 
  2.         { 
  3.             if(showSoftInput&&!bCallTextEdit) 
  4.             { 
  5.                 mScrollView.scrollBy(0, 50-dy); 
  6.                                 showSoftInput = false
  7.             } 
  8.             else if(showSoftInput&&bCallTextEdit) 
  9.             { 
  10.                 callTimeCount += 1
  11.                 if(callTimeCount == 2
  12.                 { 
  13.                 bCallTextEdit = false
  14.                 callTimeCount = 0
  15.                 } 
  16.             } 
 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参数的改变来监听我们的布局改变,那么我们自己来设置参数来控制相应的状态,从而实现实时监听。具体的细节就不再叙述了,感觉写的有点乱。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值