[英]Android: force keyboard to appear and focus on EditText
在此先感謝您的幫助。
我想在最后或執行以下代碼時出現鍵盤。
// edit text
EditText editText = new EditText(activity);
editText.setBackgroundColor(Color.WHITE);
editText.setGravity(Gravity.TOP);
editText.setText("Type here...");
RelativeLayout relativeLayoutEditText = new RelativeLayout(activity);
RelativeLayout.LayoutParams paramRelativeLayoutEditText = new RelativeLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 43 * display.getHeight() / 80 );
paramRelativeLayoutEditText.addRule(RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutEditText.addView(editText,paramRelativeLayoutEditText);
// add all created views to rootView
rootView.addView(relativeLayoutEditText, paramEditText);
// open keyboard
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
但鍵盤只在觸摸editText字段后出現(即用我的手指)。 有沒有辦法讓我可以在不使用物理觸摸的情況下自動顯示電路板?
順便說一句,我知道我如何指定寬度和高度並不是正確的做事方式。
感謝@ Shubham的鏈接,我能夠弄明白。 然而,解決方案不是鏈接中給出的答案。 這是第二個答案。
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
編輯:
一旦使用上述解決方案,鍵盤將保留在屏幕上,直到用戶按下后退按鈕或主頁按鈕(有時需要幾次)。 要刪除鍵盤,請使用此功能
imm.toggleSoftInputFromWindow(rootView.getWindowToken(), 0,0);
在我的情況下,rootView是當前活動的rootView。 我沒有測試過這個,看看這是否適用於子視圖。
加
editText.requestFocus();
試試這個:
EditText editText = (EditText ) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
確保edittext在觸摸模式下可聚焦。 你可以兩種方式做到這一點。
在xml中:
android:focusableInTouchMode="true"
在Java中:
editText.setFocusableInTouchMode(true);
試試這個
EditText textView = (EditText ) findViewById(R.id.myTextViewId);
textView.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
http://developer.android.com/reference/android/view/View.html#requestFocus()
可能的答案鏈接 。 以上許多內容也來自此鏈接。
試試這個 :
EditText textView = (EditText )findViewById(R.id.myTextViewId);
textView.requestFocus();
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(textView, InputMethodManager.SHOW_IMPLICIT);
這將顯示鍵盤
InputMethodManager imm = (InputMethodManager)
OrderMainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(txtCustomerId, InputMethodManager.SHOW_IMPLICIT);
如果您從監聽器調用鍵盤,請輸入您的活動名稱而不是此名稱。 按鈕單擊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.