Android issue with EditText object
I have an activity where the user is presented with a list of edit texts. The problem is, as soon as the activity starts, the virtual keyboard is brought up right away for the first edit text. I don't want this to happen as there is a spinner before this edit text, and often I've found that popping up the keyboard like this makes the user forget about that previous spinner.
Here's the relevant code:
<Spinner
android:id="@+id/event_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30px"
android:prompt="@string/location_prompt" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Event Name: " />
<EditText
android:id="@+id/event_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activity Type: " />
<Spinner
android:id="@+id/event_type"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address: " />
<EditText
a开发者_运维技巧ndroid:id="@+id/event_address"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Summary: activity with an EditText starts, and the virtual keyboard automatically pops up. I don't want it to do this unless the user presses on the edit text.
Thanks for any help.
The EditText is automatically set to focus so you need to put android:focusable="false"
or editText.setFocusable(false)
to prevent the EditText to be focused while the spinner is visible. You could also try editText.setSelected(false)
.
Try changing your spinner XML to this:
<Spinner
android:layout_marginTop="30px"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/event_location"
android:prompt="@string/location_prompt">
<requestFocus />
</Spinner>
Also try to do for each EditText:
android:focusable="false"
android:focusableInTouchMode="false"
Look at this post: Stop EditText from gaining focus at Activity startup
This is how it works for me:
- to make sure the edit text wont get focus on startup:
In xml set the focusable attribute of the EditText to false:
android:focusable="false"
2. in order to make the EditText focusable again after startup:
set the focusable attribute from code in the oncreate()
method of the wanted activity after the setContent()
method:
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main_view);
....
EditText et = (EditText) findViewById(R.id.et_notes);
et.setFocusable(true);
et.setFocusableInTouchMode(true);
...
}
精彩评论