如何在Android中判断软键盘是否弹出或隐藏

 我来答
撕烂你的贝塔cx
2016-03-30 · TA获得超过3943个赞
知道大有可为答主
回答量:2061
采纳率:70%
帮助的人:1274万
展开全部

自定义RelativeLayout

[java]
package com.demo.softkeyboard;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class KeyboardListenRelativeLayout extends RelativeLayout {

private static final String TAG = KeyboardListenRelativeLayout.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 = false;
private boolean mHasKeyboard = false;
private int mHeight;

private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;

public KeyboardListenRelativeLayout(Context context) {
super(context);
}
public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {
this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;
}

@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(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}

if(mHasInit && mHeight > b) {
mHasKeyboard = true;
if(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);
}
}
if(mHasInit && mHasKeyboard && mHeight == b) {
mHasKeyboard = false;
if(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);
}
}
}

public interface IOnKeyboardStateChangedListener {
public void onKeyboardStateChanged(int state);
}
}
package com.demo.softkeyboard;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

public class KeyboardListenRelativeLayout extends RelativeLayout {

private static final String TAG = KeyboardListenRelativeLayout.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 = false;
private boolean mHasKeyboard = false;
private int mHeight;

private IOnKeyboardStateChangedListener onKeyboardStateChangedListener;

public KeyboardListenRelativeLayout(Context context) {
super(context);
}
public KeyboardListenRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public KeyboardListenRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

public void setOnKeyboardStateChangedListener(IOnKeyboardStateChangedListener onKeyboardStateChangedListener) {
this.onKeyboardStateChangedListener = onKeyboardStateChangedListener;
}

@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(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_INIT);
}
} else {
mHeight = mHeight < b ? b : mHeight;
}

if(mHasInit && mHeight > b) {
mHasKeyboard = true;
if(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_SHOW);
}
}
if(mHasInit && mHasKeyboard && mHeight == b) {
mHasKeyboard = false;
if(onKeyboardStateChangedListener != null) {
onKeyboardStateChangedListener.onKeyboardStateChanged(KEYBOARD_STATE_HIDE);
}
}
}

public interface IOnKeyboardStateChangedListener {
public void onKeyboardStateChanged(int state);
}
}

用法:

[java]
package com.demo.softkeyboard;

import com.demo.softkeyboard.KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class SoftKeyboardListenDemoActivity extends Activity {

private EditText editText;
KeyboardListenRelativeLayout relativeLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

relativeLayout = (KeyboardListenRelativeLayout) findViewById(R.id.keyboardRelativeLayout);
editText = (EditText) findViewById(R.id.editText);

relativeLayout.setOnKeyboardStateChangedListener(new IOnKeyboardStateChangedListener() {

public void onKeyboardStateChanged(int state) {
switch (state) {
case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE://软键盘隐藏
editText.setVisibility(View.VISIBLE);
break;
case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW://软键盘显示
editText.setVisibility(View.GONE);
break;
default:
break;
}
}
});
}
}
package com.demo.softkeyboard;

import com.demo.softkeyboard.KeyboardListenRelativeLayout.IOnKeyboardStateChangedListener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class SoftKeyboardListenDemoActivity extends Activity {

private EditText editText;
KeyboardListenRelativeLayout relativeLayout;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

relativeLayout = (KeyboardListenRelativeLayout) findViewById(R.id.keyboardRelativeLayout);
editText = (EditText) findViewById(R.id.editText);

relativeLayout.setOnKeyboardStateChangedListener(new IOnKeyboardStateChangedListener() {

public void onKeyboardStateChanged(int state) {
switch (state) {
case KeyboardListenRelativeLayout.KEYBOARD_STATE_HIDE://软键盘隐藏
editText.setVisibility(View.VISIBLE);
break;
case KeyboardListenRelativeLayout.KEYBOARD_STATE_SHOW://软键盘显示
editText.setVisibility(View.GONE);
break;
default:
break;
}
}
});
}
}
使用到的布局文件:

[html] view plaincopyprint?<?xml version="1.0" encoding="utf-8"?>
< com.demo.softkeyboard.KeyboardListenRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardRelativeLayout"
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">
<EditText android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
< /com.demo.softkeyboard.KeyboardListenRelativeLayout>
< ?xml version="1.0" encoding="utf-8"?>
< com.demo.softkeyboard.KeyboardListenRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/keyboardRelativeLayout"
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">
<EditText android:id="@+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
< /com.demo.softkeyboard.KeyboardListenRelativeLayout>

百度网友1b00176a
2016-03-22 · 超过28用户采纳过TA的回答
知道答主
回答量:87
采纳率:50%
帮助的人:38.1万
展开全部
Android中判断软键盘是否弹出或隐藏可以借助软键盘显示和隐藏时,对主窗口进行了重新布局这个特性来进行侦听。如果我们设置的模式为压缩模式,那么我们可以对布局的onSizeChanged函数进行跟踪,如果为平移模式,那么该函数可能不会被调用。
假设跟布局为线性布局,模式为压缩模式,我们写一个例子,当输入法弹出时隐藏某个view,输入法隐藏时显示某个view。

public class ResizeLayout extends LinearLayout{
private OnResizeListener mListener;
public interface OnResizeListener {
void OnResize(int w, int h, int oldw, int oldh);
}
public void setOnResizeListener(OnResizeListener l) {
mListener = l;
}
public ResizeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh);
if (mListener != null) {
mListener.OnResize(w, h, oldw, oldh);
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
无涯shine
2016-03-22 · TA获得超过3297个赞
知道小有建树答主
回答量:1320
采纳率:80%
帮助的人:384万
展开全部
(1):虚拟键盘弹出后一般会占据当前activity的大部分,所以肉眼可以看到;
(2):代码中判断:
//判断隐藏软键盘是否弹出
if(getWindow().getAttributes().softInputMode==WindowManager.LayoutParamsSOFT_INPUT_STATE_UNSPECIFIED)
{
//隐藏软键盘
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式