I should describe what my complete scenario is. I have several Fragments in my Activity, all but one of which are hidden at any given time. When a Fragment with an EditText is getting hidden, I want to ideally keep the virtual keyboard if the next Fragment being unhidden also has an EditText. The virtual keyboard should get hidden if the next Fragment being unhidden does not have any EditText. Trying to keep track of which Fragments have EditTexts is certainly not hard, but it seems surprising that I should have to do this manually. I was expecting a call to clearFocus on the View in the Fragment being hidden would automatically do the right thing.
On May 25, 4:01 pm, Shri <[email protected]> wrote: > I have a simple layout with two EditTexts and a button. When the > Activity starts, the first EditText automatically gets focus and the > virtual keyboard is shown. When I click the button, the second > EditText gets focus because the first one is gone. If I click the > button again, the second EditText is gone, and I would expect the > virtual keyboard to go away. Clicking the button one more time shows > that #getCurrentFocus() now returns null. However, the virtual > keyboard is still displayed. > > Is this by design? Do I need to manually hide and display the virtual > keyboard myself when I hide and unhide Views? > > public void onClick(View v) { > android.util.Log.i("tag", getCurrentFocus().toString()); // > prints text_box1, text_box2, and then null > if (findViewById(R.id.edit_text1).getVisibility() == View.VISIBLE) > { > findViewById(R.id.edit_text1).setVisibility(View.GONE); > findViewById(R.id.edit_text1).clearFocus(); > } else { > findViewById(R.id.edit_text2).setVisibility(View.GONE); > findViewById(R.id.edit_text2).clearFocus(); > } > } > > <?xml version="1.0" encoding="utf-8"?> > <LinearLayout xmlns:android="http://schemas.android.com/apk/res/ > android" > android:orientation="vertical" > android:layout_width="match_parent" > android:layout_height="match_parent" > > > <EditText android:id="@+id/edit_text1" > android:layout_width="300dip" > android:layout_height="64dip" /> > <EditText android:id="@+id/edit_text2" > android:layout_width="300dip" > android:layout_height="64dip" /> > <Button > android:layout_width="300dip" > android:layout_height="64dip" > android:text="Click me" > android:onClick="onClick" /> > </LinearLayout> -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

