[英]Android - EditText made programmatically not showing keyboard
我正在制作一個使用ArrayAdapter<String>
的應用程序,並且我正在以編程方式在相對布局內創建一個 editText 視圖,該視圖全部位於AppCompatDialogFragment
內。 每個editTexts都出現了,我可以點擊它們,但如果它沒有打開鍵盤輸入。 我試過這樣的事情:
EditText editText = (EditText) findViewById(R.id.myTextViewId);
editText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
即使我嘗試手動打開鍵盤,它仍然無法打開。
RelativeLayout listLayout = new RelativeLayout(this.getContext());
listLayout.setLayoutParams(new AbsListView.LayoutParams(
AbsListView.LayoutParams.WRAP_CONTENT,
75));
if(super.getItem(position) == ParameterTypes.String.toString()) {
EditText editText = new EditText(this.getContext());
editText.setWidth(500);
editText.setHint(parameterNames[position]);
editText.setInputType(InputType.TYPE_CLASS_TEXT);
listLayout.addView(editText);
} else if(super.getItem(position) == ParameterTypes.Integer.toString()) {
EditText editText = new EditText(this.getContext());
editText.setWidth(500);
editText.setHint(parameterNames[position]);
editText.setInputType(InputType.TYPE_CLASS_NUMBER);
listLayout.addView(editText);
} else if(super.getItem(position) == ParameterTypes.Double.toString()) {
EditText editText = new EditText(this.getContext());
editText.setWidth(500);
editText.setHint(parameterNames[position]);
editText.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
listLayout.addView(editText);
return listLayout;
嘗試這個
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
在 xml
android:focusableInTouchMode="true"
您可以嘗試使用new Handler().postDelayed(new Runnable(){...}, 100L);
調用imm.showSoftInput [...]
。 因為requestFocus()
生效需要時間,尤其是當您的應用程序剛剛啟動/您的視圖時。 您還可以嘗試以相同的方式調用requestFocus()
延遲。
您可以以編程方式設置焦點:
editText.setFocusableInTouchMode(true);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.