? ? ? ?在很多Android App 開發(fā)的過(guò)程中访敌,需要對(duì)Activity 中 軟鍵盤的彈起和關(guān)閉進(jìn)項(xiàng)監(jiān)聽,但是Andoid中并沒有提供相對(duì)應(yīng)的api進(jìn)行監(jiān)聽轰枝, 我有一個(gè)簡(jiǎn)單的方法捅彻。
首先需要知道一些基礎(chǔ)知識(shí)
在manifest文件中可以設(shè)置Activity的android:windowSoftInputMode屬性累魔,這個(gè)屬性值常見的設(shè)置如下:android:windowSoftInputMode="stateAlwaysHidden|adjustPan"
那么這里值的含義列表如下:
1凛剥、stateUnspecified:軟鍵盤的狀態(tài)并沒有指定,系統(tǒng)將選擇一個(gè)合適的狀態(tài)或依賴于主題的設(shè)置
2蓝撇、stateUnchanged:當(dāng)這個(gè)activity出現(xiàn)時(shí)诚撵,軟鍵盤將一直保持在上一個(gè)activity里的狀態(tài)缭裆,無(wú)論是隱藏還是顯示
3、stateHidden:用戶選擇activity時(shí)砾脑,軟鍵盤總是被隱藏
4幼驶、stateAlwaysHidden:當(dāng)該Activity主窗口獲取焦點(diǎn)時(shí),軟鍵盤也總是被隱藏的
5韧衣、stateVisible:軟鍵盤通常是可見的
6盅藻、stateAlwaysVisible:用戶選擇activity時(shí),軟鍵盤總是顯示的狀態(tài)
7畅铭、adjustUnspecified:默認(rèn)設(shè)置氏淑,通常由系統(tǒng)自行決定是隱藏還是顯示
8、adjustResize:該Activity總是調(diào)整屏幕的大小以便留出軟鍵盤的空間
9硕噩、adjustPan:當(dāng)前窗口的內(nèi)容將自動(dòng)移動(dòng)以便當(dāng)前焦點(diǎn)從不被鍵盤覆蓋和用戶能總是看到輸入內(nèi)容的部分
案例:
1.我們需要將監(jiān)聽所在的Activity在Manifest文件中的設(shè)置為如下形式:
<activity?
android:name="com.zy.project.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>
當(dāng)有軟鍵盤彈起時(shí)假残,Activity的布局大小會(huì)被滾動(dòng)上去,但是你仍然可以通過(guò)滑動(dòng)瀏覽所有。
2 需要在外層布局文件設(shè)置一個(gè)id辉懒,并在activity 中設(shè)置監(jiān)聽
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity implements OnLayoutChangeListener{
//Activity最外層的Layout視圖
private View rootView;
//屏幕高度
private int screenHeight = 0;
//軟件盤彈起后所占高度閥值 一般是占用屏幕的1/3
private int keyHeight = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rootView = 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)聽器
rootView.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)值阳惹,沒有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)為軟鍵盤彈起
if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > keyHeight)){
Toast.makeText(MainActivity.this, "監(jiān)聽到軟鍵盤彈起...", Toast.LENGTH_SHORT).show();
}else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > keyHeight)){
Toast.makeText(MainActivity.this, "監(jiān)聽到軟件盤關(guān)閉...", Toast.LENGTH_SHORT).show();
}
}
本文來(lái)自:http://m.blog.csdn.net/bear_huangzhen/article/details/45896333