因爲需求,進入頁面要你自動彈出鍵盤
查了資料,試了發現無論怎麼改方法都不能 彈出
最後給控件加了定時器,延時1秒,居然可以了
private void showKeyboard(Context context, final View view){
// 必須給控件加這個方法,否則無效
view.requestFocus();
Timer timer = new Timer(); //設置定時器
timer.schedule(new TimerTask() {
@Override
public void run() { //彈出軟鍵盤的代碼
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view, InputMethodManager.RESULT_SHOWN);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
}
}, 1000); //設置1000毫秒的時長
}
想了一下,google 的方法應該是頁面都加載完成纔可以彈出 鍵盤
順便在放上收起的方法
/**
* 隱藏軟鍵盤
*
* @param context
* @param view
*/
public static void hideSoftKeyboard(Context context, View view) {
if (view == null)
return;
InputMethodManager inputMethodManager = (InputMethodManager) context.getSystemService(
Context.INPUT_METHOD_SERVICE);
if (inputMethodManager.isActive())
inputMethodManager.hideSoftInputFromWindow(
view.getWindowToken(), 0);
}
特記錄該問題,給有這個需求的小夥伴提供個方案,少入點坑