Android軟鍵盤狀態(tài)彈出與消失的示例
最近遇到了關(guān)于軟鍵盤的問(wèn)題,需要獲取到軟鍵盤的狀態(tài),是否在顯示 ,記錄一下,方便以后查閱。網(wǎng)上常見(jiàn)的判定狀態(tài)方法
getWindow().getAttributes().softInputMode== WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
來(lái)判斷軟鍵盤是否打開(kāi),若相等則為打開(kāi)。試了之后,發(fā)現(xiàn)這個(gè)只對(duì)手機(jī)自帶的鍵盤有作用,對(duì)安裝的第三方的輸入法沒(méi)有效果。
還有介紹使用InputMethodManager 來(lái)獲取鍵盤狀態(tài),代碼如下
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); boolean isOpen=imm.isActive();//isOpen若返回true,則表示輸入法打開(kāi)
這種并不能實(shí)時(shí)獲取到鍵盤的狀態(tài),對(duì)我依然沒(méi)有效果。
后來(lái)找到的解決方法,監(jiān)聽(tīng)屏幕的變化,代碼如下:
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
/**
*
* 軟鍵盤的監(jiān)聽(tīng)
*/
public class KeyBoardShowListener {
private Context ctx;
public KeyBoardShowListener(Context ctx) {
this.ctx = ctx;
}
OnKeyboardVisibilityListener keyboardListener;
public OnKeyboardVisibilityListener getKeyboardListener() {
return keyboardListener;
}
public interface OnKeyboardVisibilityListener {
void onVisibilityChanged(boolean visible);
}
public void setKeyboardListener(final OnKeyboardVisibilityListener listener, Activity activity) {
final View activityRootView = ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
private boolean wasOpened;
private final int DefaultKeyboardDP = 100;
// From @nathanielwolf answer... Lollipop includes button bar in the root. Add height of button bar (48dp) to maxDiff
private final int EstimatedKeyboardDP = DefaultKeyboardDP + (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? 48 : 0);
private final Rect r = new Rect();
@Override
public void onGlobalLayout() {
// Convert the dp to pixels.
int estimatedKeyboardHeight = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, EstimatedKeyboardDP, activityRootView.getResources().getDisplayMetrics());
// Conclude whether the keyboard is shown or not.
activityRootView.getWindowVisibleDisplayFrame(r);
int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
boolean isShown = heightDiff >= estimatedKeyboardHeight;
if (isShown == wasOpened) {
Log.e("Keyboard state", "Ignoring global layout change...");
return;
}
wasOpened = isShown;
listener.onVisibilityChanged(isShown);
}
});
}
}
用法如下:
//監(jiān)聽(tīng)軟鍵盤的狀態(tài)
new KeyBoardShowListener(Activity.this).setKeyboardListener(
new KeyBoardShowListener.OnKeyboardVisibilityListener() {
@Override
public void onVisibilityChanged(boolean visible) {
if (visible) {
//軟鍵盤已彈出
} else {
//軟鍵盤未彈出
}
}
}, Activity.this);
以下是可能會(huì)遇到的一些情況:
綁定軟鍵盤到EditText
edit.setFocusable(true); edit.setFocusableInTouchMode(true); edit.requestFocus(); InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(edit, 0);
去除軟鍵盤顯示:
editMsgView.setText("");
editMsgView.clearFocus();
//close InputMethodManager
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);
始終不彈出軟件鍵盤
EditText edit=(EditText)findViewById(R.id.edit); edit.setInputType(InputType.TYPE_NULL);
也可以:
InputMethodManager imm = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm.isActive()){ //這里可以判斷也可以不判斷
imm.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0 );
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析android中隱藏與顯示軟鍵盤及不自動(dòng)彈出鍵盤的實(shí)現(xiàn)方法
- Android中監(jiān)聽(tīng)軟鍵盤顯示狀態(tài)實(shí)現(xiàn)代碼
- Android 顯示和隱藏軟鍵盤的方法(手動(dòng))
- Android軟鍵盤彈出時(shí)的界面控制方法
- Android開(kāi)發(fā)軟鍵盤遮擋登陸按鈕的完美解決方案
- Android編程之軟鍵盤的隱藏顯示實(shí)例詳解
- Android開(kāi)發(fā)之軟鍵盤用法實(shí)例分析
- Android軟鍵盤遮擋的四種完美解決方案
- Android判斷軟鍵盤彈出并隱藏的簡(jiǎn)單完美解決方法(推薦)
- 頁(yè)面未隨軟鍵盤上升及android隱藏軟鍵盤總結(jié)
- Android判斷軟鍵盤的狀態(tài)和隱藏軟鍵盤的簡(jiǎn)單實(shí)例
相關(guān)文章
詳解Android中常見(jiàn)的內(nèi)存優(yōu)化及內(nèi)存泄露場(chǎng)景
本文主要給大家介紹了Android中常見(jiàn)的內(nèi)存優(yōu)化及Android開(kāi)發(fā)中容易造成內(nèi)存泄露的場(chǎng)景,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-08-08
Android Fragment動(dòng)態(tài)創(chuàng)建詳解及示例代碼
這篇文章主要介紹了Android Fragment動(dòng)態(tài)創(chuàng)建詳解的相關(guān)資料,并附實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11
Flutter開(kāi)發(fā)之對(duì)角棋游戲?qū)崿F(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Flutter開(kāi)發(fā)之對(duì)角棋游戲?qū)崿F(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
android 仿ios數(shù)字密碼解鎖界面的實(shí)例
下面小編就為大家分享一篇android 仿ios數(shù)字密碼解鎖界面的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Android實(shí)現(xiàn)實(shí)時(shí)通信示例
本篇文章主要介紹了Android實(shí)時(shí)通信示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03

