android监听键盘隐藏事件,听android键盘显示或隐藏事件

请尝试以下代码: –

// from the link above

@Override

public void onConfigurationChanged(Configuration newConfig) {

super.onConfigurationChanged(newConfig);

// Checks whether a hardware keyboard is available

if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {

Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();

} else if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_YES) {

Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();

}

}

要么

boolean isOpened = false;

public void setListnerToRootView(){

final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);

activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {

@Override

public void onGlobalLayout() {

int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();

if (heightDiff > 100 ) { // 99% of the time the height diff will be due to a keyboard.

Toast.makeText(getApplicationContext(), "Gotcha!!! softKeyboardup", 0).show();

if(isOpened == false){

//Do two things, make the view top visible and the editText smaller

}

isOpened = true;

}else if(isOpened == true){

Toast.makeText(getApplicationContext(), "softkeyborad Down!!!", 0).show();

isOpened = false;

}

}

});

}

要么

对于以下代码,您必须扩展LinearLayout.

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

final int proposedheight = MeasureSpec.getSize(heightMeasureSpec);

final int actualHeight = getHeight();

if (actualHeight > proposedheight){

// Keyboard is shown

} else {

// Keyboard is hidden

}

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

}

见下面的链接:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值