android检测软键盘是否弹起
2014-04-24 14:37
363 查看
android.view.ViewGroup
protected void onLayout(boolean changed, int l, int t, int r, int b)
执行layout操作时调用onLayout方法。View要给它的每个Child设定size和position。拥有Children的子类需要重写onLayout方法并且调用每个Child的layout方法。
参数changed表示view的size或position发生变化。参数l, t, r, b分别表示相对于parent的left, top, right, bottom position。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
测量View及其Content,确定measuredWidth和measuredHeight。在方法measure(int, int)中调用。重写onMeasure方法时,需要调用方法setMeasuredDimension(int, int),存储View的measuredWidth和measuredHeight。若存储失败,方法measure(int, int)会抛出异常IllegalStateException。可以调用super.onMeasure(int, int)方法。
除非MeasureSpec准许更大的size,否则measure的默认实现是background size。子类重写onMeasure(int, int)提供Content的更佳测量。如果onMeasure被重写,子类必须保证measuredWidth和measuredHeight至少是view的minHeight和minWidth。minHeight/Width通过getSuggestedMinimumHight/Width()获取。
参数width/heightMeasureSpec表示parent强加的horizontal/vertical space要求。
void android.view.ViewGroup.layout(int l, int t, int r, int b)
给view及其descendants设定size和position。它正是layout机制的第2个步,每个parent调用它的chlidren的layout操作。使用在measure阶段测量得到的size和position数据完成layout操作。拥有child的子类必须重写onLayout方法,调用每个child的layout操作。
void android.view.ViewGroup.measureChildren(int widthMeasureSpec, int heightMeasureSpec)
请View的所有children测量themseles, 测量依据是View的MeasureSpec和padding。忽略处于GONE状态的children,是否GONE状态由getChildMeasureSpec来确定。
参数width/heightMeasureSpec表示view的width/height要求。
使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。
package com.ransj.keyboard;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
public class KeyboardLayout extends RelativeLayout {
private static final String TAG = KeyboardLayout.class.getSimpleName();
public static final byte KEYBOARD_STATE_SHOW = -3;
public static final byte KEYBOARD_STATE_HIDE = -2;
public static final byte KEYBOARD_STATE_INIT = -1;
private boolean mHasInit;
private boolean mHasKeybord;
private int mHeight;
private onKybdsChangeListener mListener;
public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public KeyboardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardLayout(Context context) {
super(context);
}
/**
* set keyboard state listener
*/
public void setOnkbdStateListener(onKybdsChangeListener listener){
mListener = listener;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (!mHasInit) {
mHasInit = true;
mHeight = b;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}
if (mHasInit && mHeight > b) {
mHasKeybord = true;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
}
Log.w(TAG, "show keyboard.......");
}
if (mHasInit && mHasKeybord && mHeight == b) {
mHasKeybord = false;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
}
Log.w(TAG, "hide keyboard.......");
}
}
public interface onKybdsChangeListener{
public void onKeyBoardStateChange(int state);
}
}
复制代码
这个是自定义的布局,自定义布局可以继承各种常见布局。自定义布局有键盘状态改变监听器,可以通过注册监听器来监听软键盘状态。
<?xml version="1.0" encoding="utf-8"?>
<com.ransj.keyboard.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/testone_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:text="软件盘弹起,我将消失!软键盘隐藏,我将回来!"
android:layout_weight="1.0"
android:textColor="#00ff00"
android:textStyle="bold" />
<EditText
android:id="@+id/testone_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
</ScrollView>
</com.ransj.keyboard.KeyboardLayout>
复制代码
这个是优化页面的布局,从上面可以看出自定义布局的用法。
package com.ransj.keyboard;
import com.ransj.keyboard.KeyboardLayout.onKybdsChangeListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
*
*
*/
public class TestOne extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testone);
KeyboardLayout mainView = (KeyboardLayout) findViewById(R.id.keyboardLayout1);
final TextView tv = (TextView) findViewById(R.id.testone_tv);
mainView.setOnkbdStateListener(new onKybdsChangeListener() {
public void onKeyBoardStateChange(int state) {
switch (state) {
case KeyboardLayout.KEYBOARD_STATE_HIDE:
tv.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
break;
case KeyboardLayout.KEYBOARD_STATE_SHOW:
tv.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
}
复制代码
这个是优化页面的代码逻辑,通过注册监听器,来监听软键盘事件。在软键盘弹起时隐藏Textview,在软键盘隐藏时显示Textview。
msg_send_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout() {
int total_msg_height=msg_send_view.getRootView().getHeight()-msg_send_view.getHeight();
LogHelper.info("******************************2布局高度:"+currentHeight);//获取当前短信布局高度
LogHelper.info("******************************2屏幕高度:"+globleHeight);//获取当前短信布局高度
LogHelper.info("******************************2适配器高度:"+msgHeight);//获取当前短信布局高度
msgHeight=mPullRefreshListView.getMeasuredHeight();
if(total_msg_height>100){
if(currentHeight>msgHeight){
actualListView.setStackFromBottom(true);
}
else{
actualListView.setStackFromBottom(false);
}
}else{
if(currentHeight>globleHeight){
actualListView.setStackFromBottom(true);
}
else{
actualListView.setStackFromBottom(false);
}
}
}
});
}
protected void onLayout(boolean changed, int l, int t, int r, int b)
执行layout操作时调用onLayout方法。View要给它的每个Child设定size和position。拥有Children的子类需要重写onLayout方法并且调用每个Child的layout方法。
参数changed表示view的size或position发生变化。参数l, t, r, b分别表示相对于parent的left, top, right, bottom position。
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
测量View及其Content,确定measuredWidth和measuredHeight。在方法measure(int, int)中调用。重写onMeasure方法时,需要调用方法setMeasuredDimension(int, int),存储View的measuredWidth和measuredHeight。若存储失败,方法measure(int, int)会抛出异常IllegalStateException。可以调用super.onMeasure(int, int)方法。
除非MeasureSpec准许更大的size,否则measure的默认实现是background size。子类重写onMeasure(int, int)提供Content的更佳测量。如果onMeasure被重写,子类必须保证measuredWidth和measuredHeight至少是view的minHeight和minWidth。minHeight/Width通过getSuggestedMinimumHight/Width()获取。
参数width/heightMeasureSpec表示parent强加的horizontal/vertical space要求。
void android.view.ViewGroup.layout(int l, int t, int r, int b)
给view及其descendants设定size和position。它正是layout机制的第2个步,每个parent调用它的chlidren的layout操作。使用在measure阶段测量得到的size和position数据完成layout操作。拥有child的子类必须重写onLayout方法,调用每个child的layout操作。
void android.view.ViewGroup.measureChildren(int widthMeasureSpec, int heightMeasureSpec)
请View的所有children测量themseles, 测量依据是View的MeasureSpec和padding。忽略处于GONE状态的children,是否GONE状态由getChildMeasureSpec来确定。
参数width/heightMeasureSpec表示view的width/height要求。
使用自定义布局,页面布局中包含ScrollVIew,在软键盘弹起后,布局的高度会发生改变,根据布局的高度来判断软键盘的状态。
package com.ransj.keyboard;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;
public class KeyboardLayout extends RelativeLayout {
private static final String TAG = KeyboardLayout.class.getSimpleName();
public static final byte KEYBOARD_STATE_SHOW = -3;
public static final byte KEYBOARD_STATE_HIDE = -2;
public static final byte KEYBOARD_STATE_INIT = -1;
private boolean mHasInit;
private boolean mHasKeybord;
private int mHeight;
private onKybdsChangeListener mListener;
public KeyboardLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public KeyboardLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public KeyboardLayout(Context context) {
super(context);
}
/**
* set keyboard state listener
*/
public void setOnkbdStateListener(onKybdsChangeListener listener){
mListener = listener;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (!mHasInit) {
mHasInit = true;
mHeight = b;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}
if (mHasInit && mHeight > b) {
mHasKeybord = true;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_SHOW);
}
Log.w(TAG, "show keyboard.......");
}
if (mHasInit && mHasKeybord && mHeight == b) {
mHasKeybord = false;
if (mListener != null) {
mListener.onKeyBoardStateChange(KEYBOARD_STATE_HIDE);
}
Log.w(TAG, "hide keyboard.......");
}
}
public interface onKybdsChangeListener{
public void onKeyBoardStateChange(int state);
}
}
复制代码
这个是自定义的布局,自定义布局可以继承各种常见布局。自定义布局有键盘状态改变监听器,可以通过注册监听器来监听软键盘状态。
<?xml version="1.0" encoding="utf-8"?>
<com.ransj.keyboard.KeyboardLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:fillViewport="true" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/testone_tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center"
android:text="软件盘弹起,我将消失!软键盘隐藏,我将回来!"
android:layout_weight="1.0"
android:textColor="#00ff00"
android:textStyle="bold" />
<EditText
android:id="@+id/testone_et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
</ScrollView>
</com.ransj.keyboard.KeyboardLayout>
复制代码
这个是优化页面的布局,从上面可以看出自定义布局的用法。
package com.ransj.keyboard;
import com.ransj.keyboard.KeyboardLayout.onKybdsChangeListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
*
*
*/
public class TestOne extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testone);
KeyboardLayout mainView = (KeyboardLayout) findViewById(R.id.keyboardLayout1);
final TextView tv = (TextView) findViewById(R.id.testone_tv);
mainView.setOnkbdStateListener(new onKybdsChangeListener() {
public void onKeyBoardStateChange(int state) {
switch (state) {
case KeyboardLayout.KEYBOARD_STATE_HIDE:
tv.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "软键盘隐藏", Toast.LENGTH_SHORT).show();
break;
case KeyboardLayout.KEYBOARD_STATE_SHOW:
tv.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "软键盘弹起", Toast.LENGTH_SHORT).show();
break;
}
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
}
}
复制代码
这个是优化页面的代码逻辑,通过注册监听器,来监听软键盘事件。在软键盘弹起时隐藏Textview,在软键盘隐藏时显示Textview。
msg_send_view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener()
{
@Override
public void onGlobalLayout() {
int total_msg_height=msg_send_view.getRootView().getHeight()-msg_send_view.getHeight();
LogHelper.info("******************************2布局高度:"+currentHeight);//获取当前短信布局高度
LogHelper.info("******************************2屏幕高度:"+globleHeight);//获取当前短信布局高度
LogHelper.info("******************************2适配器高度:"+msgHeight);//获取当前短信布局高度
msgHeight=mPullRefreshListView.getMeasuredHeight();
if(total_msg_height>100){
if(currentHeight>msgHeight){
actualListView.setStackFromBottom(true);
}
else{
actualListView.setStackFromBottom(false);
}
}else{
if(currentHeight>globleHeight){
actualListView.setStackFromBottom(true);
}
else{
actualListView.setStackFromBottom(false);
}
}
}
});
}
相关文章推荐
- android 检测软键盘的弹起与隐藏【绝对经典,好用】
- Android检测软键盘的弹起与隐藏
- Android 检测软键盘的弹起与隐藏
- Android常用工具方法(关闭软键盘,安装apk,检测程序是否安装...)
- Android和IOS的浏览器中检测是否安装某个客户端的方法
- Android检测网络是否可用和主机是否可达
- Android下检测手机移动网络、WIFI网络是否开启或链接
- 如何在Android真机上检测是否有Google Map add-on
- 如何在Android真机上检测是否有Google Map add-on[转载]
- android检测当前网络是否可用
- [置顶] Android开发必有功能,更新版本提示,检测是否有新版本更新。下载完成后进行安装。
- Android检测软键盘显示状态,隐藏软件键盘
- Android程序检测网络是否可用
- Android 检测设备当前网络是否可用
- Android 软键盘的监听(监听高度,是否显示)
- Android中检测当前是否为主线程
- Android之监听手机软键盘弹起与关闭
- Android检测Activity或者Service是否运行
- android 程序检测是否安装其他一个程序
- android 检测耳机是否插入