我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
我在Android中有一个活动,包含两个元素:
编辑文本列表视图
当“活动”启动时,EditText立即具有输入焦点(闪烁光标)。我不希望任何控件在启动时具有输入焦点。我尝试了:
EditText.setSelected(false);
EditText.setFocusable(false);
没有运气。如何说服EditText在“活动”启动时不选择自己?
当前回答
在父版面中写入此行。。。
android:focusableInTouchMode="true"
其他回答
在第一个可编辑字段之前尝试以下操作:
<TextView
android:id="@+id/dummyfocus"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/foo"
/>
findViewById(R.id.dummyfocus).setFocusableInTouchMode(true);
findViewById(R.id.dummyfocus).requestFocus();
您有编辑文本和列表。在OnStart/On Create中,应该将焦点设置在listview:listview.requestfocus()上
在Manifest.xml文件的活动标记中添加android:windowSoftInputMode=“stateAlwaysHidden”。
来源
您可以在布局的第一个TextView中将“focusable”和“focustable in touch mode”设置为true。这样,当活动开始时,TextView将被聚焦,但由于其性质,您将不会在屏幕上看到任何聚焦,当然,也不会显示键盘。。。
在onCreate()中禁用它
final KeyListener edtTxtMessageKeyListener = edtTxtMessage.getKeyListener();
edtTxtMessage.setCursorVisible(false);
edtTxtMessage.setKeyListener(null);
最后在EditText的onClick()中启用它
edtTxtMessage.setCursorVisible(true);
edtTxtMessage.setKeyListener(edtTxtMessageKeyListener);
但问题是,我们必须在第一次使用OnScreenKeyboard时单击两下。
@变通办法
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
也可以在onClick()中尝试:)