背景:
在很多App開(kāi)發(fā)過(guò)程中需要在Activity中監(jiān)聽(tīng)Android設(shè)備的軟鍵盤(pán)彈起與關(guān)閉相赁,但是Android似乎沒(méi)有提供相關(guān)的的監(jiān)聽(tīng)API給我們來(lái)調(diào)用废封,本文提供了一個(gè)可行的辦法來(lái)監(jiān)聽(tīng)軟鍵盤(pán)的彈起與關(guān)閉。
預(yù)備知識(shí):
在manifest文件中可以設(shè)置Activity的android:windowSoftInputMode屬性登夫,這個(gè)屬性值常見(jiàn)的設(shè)置如下:
android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
那么這里值的含義列表如下:
【A】stateUnspecified:軟鍵盤(pán)的狀態(tài)并沒(méi)有指定拷邢,系統(tǒng)將選擇一個(gè)合適的狀態(tài)或依賴于主題的設(shè)置
【B】stateUnchanged:當(dāng)這個(gè)activity出現(xiàn)時(shí),軟鍵盤(pán)將一直保持在上一個(gè)activity里的狀態(tài)平斩,無(wú)論是隱藏還是顯示
【C】stateHidden:用戶選擇activity時(shí)亚享,軟鍵盤(pán)總是被隱藏
【D】stateAlwaysHidden:當(dāng)該Activity主窗口獲取焦點(diǎn)時(shí),軟鍵盤(pán)也總是被隱藏的
【E】stateVisible:軟鍵盤(pán)通常是可見(jiàn)的
【F】stateAlwaysVisible:用戶選擇activity時(shí)绘面,軟鍵盤(pán)總是顯示的狀態(tài)
【G】adjustUnspecified:默認(rèn)設(shè)置欺税,通常由系統(tǒng)自行決定是隱藏還是顯示
【H】adjustResize:該Activity總是調(diào)整屏幕的大小以便留出軟鍵盤(pán)的空間
【I】adjustPan:當(dāng)前窗口的內(nèi)容將自動(dòng)移動(dòng)以便當(dāng)前焦點(diǎn)從不被鍵盤(pán)覆蓋和用戶能總是看到輸入內(nèi)容的部分
示例:
(1)首先我們需要將監(jiān)聽(tīng)所在的Activity在Manifest文件中的設(shè)置為如下形式:
<activity
android:name="com.bear.softkeyboardlistener.MainActivity"
android:label="@string/app_name"
android:windowSoftInputMode="stateAlwaysHidden|adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
這樣設(shè)置之后,當(dāng)有軟鍵盤(pán)彈起來(lái)的時(shí)候揭璃,Activity的布局大小會(huì)被壓縮上去晚凿,但是你仍然可以通過(guò)滑動(dòng)瀏覽所有。
(2)我們要為Activity的最外面的Layout設(shè)置一個(gè)OnLayoutChangeListener監(jiān)聽(tīng)器:
public class MainActivity extends Activity implements OnLayoutChangeListener{
//Activity最外層的Layout視圖
private View activityRootView;
//屏幕高度
private int screenHeight = 0;
//軟件盤(pán)彈起后所占高度閥值
private int keyHeight = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityRootView = findViewById(R.id.root_layout);
//獲取屏幕高度
screenHeight = this.getWindowManager().getDefaultDisplay().getHeight();
//閥值設(shè)置為屏幕高度的1/3
keyHeight = screenHeight/3;
}
@Override
protected void onResume() {
super.onResume();
//添加layout大小發(fā)生改變監(jiān)聽(tīng)器
activityRootView.addOnLayoutChangeListener(this);
}
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
//old是改變前的左上右下坐標(biāo)點(diǎn)值瘦馍,沒(méi)有old的是改變后的左上右下坐標(biāo)點(diǎn)值
// System.out.println(oldLeft + " " + oldTop +" " + oldRight + " " + oldBottom);
// System.out.println(left + " " + top +" " + right + " " + bottom);
//現(xiàn)在認(rèn)為只要控件將Activity向上推的高度超過(guò)了1/3屏幕高歼秽,就認(rèn)為軟鍵盤(pán)彈起
if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){
Toast.makeText(MainActivity.this, "監(jiān)聽(tīng)到軟鍵盤(pán)彈起...", Toast.LENGTH_SHORT).show();
}else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){
Toast.makeText(MainActivity.this, "監(jiān)聽(tīng)到軟件盤(pán)關(guān)閉...", Toast.LENGTH_SHORT).show();
}
}
寫(xiě)文章不易,路過(guò)的伙伴辛苦點(diǎn)個(gè)贊情组,謝謝支持燥筷!