Android軟鍵盤(pán)狀態(tài)彈出與消失的示例
最近遇到了關(guān)于軟鍵盤(pán)的問(wèn)題,需要獲取到軟鍵盤(pán)的狀態(tài),是否在顯示 ,記錄一下,方便以后查閱。網(wǎng)上常見(jiàn)的判定狀態(tài)方法
getWindow().getAttributes().softInputMode== WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED
來(lái)判斷軟鍵盤(pán)是否打開(kāi),若相等則為打開(kāi)。試了之后,發(fā)現(xiàn)這個(gè)只對(duì)手機(jī)自帶的鍵盤(pán)有作用,對(duì)安裝的第三方的輸入法沒(méi)有效果。
還有介紹使用InputMethodManager 來(lái)獲取鍵盤(pán)狀態(tài),代碼如下
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); boolean isOpen=imm.isActive();//isOpen若返回true,則表示輸入法打開(kāi)
這種并不能實(shí)時(shí)獲取到鍵盤(pán)的狀態(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; /** * * 軟鍵盤(pán)的監(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)軟鍵盤(pán)的狀態(tài) new KeyBoardShowListener(Activity.this).setKeyboardListener( new KeyBoardShowListener.OnKeyboardVisibilityListener() { @Override public void onVisibilityChanged(boolean visible) { if (visible) { //軟鍵盤(pán)已彈出 } else { //軟鍵盤(pán)未彈出 } } }, Activity.this);
以下是可能會(huì)遇到的一些情況:
綁定軟鍵盤(pán)到EditText
edit.setFocusable(true); edit.setFocusableInTouchMode(true); edit.requestFocus(); InputMethodManager inputManager = (InputMethodManager)edit.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(edit, 0);
去除軟鍵盤(pán)顯示:
editMsgView.setText(""); editMsgView.clearFocus(); //close InputMethodManager InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editMsgView.getWindowToken(), 0);
始終不彈出軟件鍵盤(pán)
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中隱藏與顯示軟鍵盤(pán)及不自動(dòng)彈出鍵盤(pán)的實(shí)現(xiàn)方法
- Android中監(jiān)聽(tīng)軟鍵盤(pán)顯示狀態(tài)實(shí)現(xiàn)代碼
- Android 顯示和隱藏軟鍵盤(pán)的方法(手動(dòng))
- Android軟鍵盤(pán)彈出時(shí)的界面控制方法
- Android開(kāi)發(fā)軟鍵盤(pán)遮擋登陸按鈕的完美解決方案
- Android編程之軟鍵盤(pán)的隱藏顯示實(shí)例詳解
- Android開(kāi)發(fā)之軟鍵盤(pán)用法實(shí)例分析
- Android軟鍵盤(pán)遮擋的四種完美解決方案
- Android判斷軟鍵盤(pán)彈出并隱藏的簡(jiǎn)單完美解決方法(推薦)
- 頁(yè)面未隨軟鍵盤(pán)上升及android隱藏軟鍵盤(pán)總結(jié)
- Android判斷軟鍵盤(pán)的狀態(tài)和隱藏軟鍵盤(pán)的簡(jiǎn)單實(shí)例
相關(guān)文章
Android自定義View模仿即刻點(diǎn)贊數(shù)字切換效果實(shí)例
有一個(gè)項(xiàng)目是仿即刻的點(diǎn)贊,這篇文章主要給大家介紹了關(guān)于Android自定義View模仿即刻點(diǎn)贊數(shù)字切換效果的相關(guān)資料,文中通過(guò)示例代碼介紹 的非常詳細(xì),需要的朋友可以參考下2022-12-12Java操作FreeMarker模板引擎的基本用法示例小結(jié)
這篇文章主要介紹了Java操作FreeMarker模板引擎的基本用法示例小結(jié),FreeMarker本身由Java寫(xiě)成,用模板來(lái)生成文本輸出,需要的朋友可以參考下2016-02-02Android實(shí)現(xiàn)對(duì)圖片放大、平移和旋轉(zhuǎn)的功能
現(xiàn)在很多App在查看一張圖片的原圖時(shí),都會(huì)支持圖片的手勢(shì)縮放,手勢(shì)平移以及圖片旋轉(zhuǎn)的操作。那么今天小編就來(lái)教大家去簡(jiǎn)單的實(shí)現(xiàn)圖片的放大、平移、旋轉(zhuǎn)的操作,有需要的可以參考借鑒。2016-08-08Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法
這篇文章主要介紹了Android使用httpPost向服務(wù)器發(fā)送請(qǐng)求的方法,實(shí)例分析了Android針對(duì)HttpPost類的操作技巧,需要的朋友可以參考下2015-12-12Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)系統(tǒng)級(jí)懸浮按鈕的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-03-03