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

本文主要是介绍解决Android编辑框在全屏模式下无法检测布局变化的问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

解决Android编辑框在全屏模式下无法检测布局变化的问题       
        分类:            Android 225人阅读 评论(0) 收藏 举报
android Android layout Layout 软键盘的显示和隐藏监听


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

[java] view plain copy print ?
  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}@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函数里面相应的参数也不会改变。此时将无法通过检测参数的数值变化来监听软键盘的显示和隐藏,从而无法在显示和隐藏软键盘时实时改变自己的布局。


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

[java] view plain copy print ?
  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参数的改变来监听我们的布局改变,那么我们自己来设置参数来控制相应的状态,从而实现实时监听。具体的细节就不再叙述了,感觉写的有点乱。

这篇关于解决Android编辑框在全屏模式下无法检测布局变化的问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1069234

相关文章

使用TomCat,service输出台出现乱码的解决

《使用TomCat,service输出台出现乱码的解决》本文介绍了解决Tomcat服务输出台中文乱码问题的两种方法,第一种方法是修改`logging.properties`文件中的`prefix`和`... 目录使用TomCat,service输出台出现乱码问题1解决方案问题2解决方案总结使用TomCat,

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

Java循环创建对象内存溢出的解决方法

《Java循环创建对象内存溢出的解决方法》在Java中,如果在循环中不当地创建大量对象而不及时释放内存,很容易导致内存溢出(OutOfMemoryError),所以本文给大家介绍了Java循环创建对象... 目录问题1. 解决方案2. 示例代码2.1 原始版本(可能导致内存溢出)2.2 修改后的版本问题在

大数据小内存排序问题如何巧妙解决

《大数据小内存排序问题如何巧妙解决》文章介绍了大数据小内存排序的三种方法:数据库排序、分治法和位图法,数据库排序简单但速度慢,对设备要求高;分治法高效但实现复杂;位图法可读性差,但存储空间受限... 目录三种方法:方法概要数据库排序(http://www.chinasem.cn对数据库设备要求较高)分治法(常

golang1.23版本之前 Timer Reset方法无法正确使用

《golang1.23版本之前TimerReset方法无法正确使用》在Go1.23之前,使用`time.Reset`函数时需要先调用`Stop`并明确从timer的channel中抽取出东西,以避... 目录golang1.23 之前 Reset ​到底有什么问题golang1.23 之前到底应该如何正确的

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

linux报错INFO:task xxxxxx:634 blocked for more than 120 seconds.三种解决方式

《linux报错INFO:taskxxxxxx:634blockedformorethan120seconds.三种解决方式》文章描述了一个Linux最小系统运行时出现的“hung_ta... 目录1.问题描述2.解决办法2.1 缩小文件系统缓存大小2.2 修改系统IO调度策略2.3 取消120秒时间限制3

关于@MapperScan和@ComponentScan的使用问题

《关于@MapperScan和@ComponentScan的使用问题》文章介绍了在使用`@MapperScan`和`@ComponentScan`时可能会遇到的包扫描冲突问题,并提供了解决方法,同时,... 目录@MapperScan和@ComponentScan的使用问题报错如下原因解决办法课外拓展总结@

MybatisGenerator文件生成不出对应文件的问题

《MybatisGenerator文件生成不出对应文件的问题》本文介绍了使用MybatisGenerator生成文件时遇到的问题及解决方法,主要步骤包括检查目标表是否存在、是否能连接到数据库、配置生成... 目录MyBATisGenerator 文件生成不出对应文件先在项目结构里引入“targetProje