1、對軟鍵盤的操作:InputMethodManager
/**
* 切換鍵盤狀態
*
* @param v
*/
public void switchInput(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
}
/**
* 打開鍵盤
*
* @param v
*/
public void openInput(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(findViewById(R.id.editText1), InputMethodManager.SHOW_IMPLICIT);
}
/**
* 關閉鍵盤
*
* @param v
*/
public void closeInput(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0); // 強制隱藏鍵盤
}
2、鍵盤彈出的佈局變化(頂上去):
(1)使用scrollview佈局
(2)、edittext的點擊事件
// 將ScrollView滾動到底
mScrollView.fullScroll(View.FOCUS_DOWN);
(3)、xml的配置
<!-- 只有用戶點擊了編輯框才顯示軟鍵盤,並且會導致原有界面重新佈局 -->
<activity
android:name="com.test.ruanjianpan.Myruanjianpan2Activity"
android:label="@string/title_activity_myruanjianpan2"
android:windowSoftInputMode="stateHidden|adjustResize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
////不能實現效果的原因:設定了固定的高度,導致被遮擋: