11
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="20dp" >

    <AutoCompleteTextView
        android:id="@+id/autocomplete_zone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:dropDownVerticalOffset="0.2dp"
        android:ems="10"
        android:hint="@string/zone_hint"
        android:inputType="text"
        android:lines="1"
        android:maxLines="1"
        android:popupBackground="#00ffffff"
        android:textColor="#ffffff"
        android:textColorHint="#ffffff" >

        <requestFocus />
    </AutoCompleteTextView>

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@id/autocomplete_zone"
        android:layout_alignRight="@id/autocomplete_zone"
        android:layout_alignTop="@id/autocomplete_zone"
        android:paddingBottom="5dp"
        android:visibility="invisible" />
</RelativeLayout>

In the above markup

<requestFocus />

does not work. The text view is not focused when the activity is started. Is this because of the progress bar which overlays the text view? Any ideas how to fix this?

4

5 回答 5

18

我通常使用以下设置焦点:将以下属性添加到您的xml-layout

<AutoCompleteTextView 
    android:focusable="true" 
    android:focusableInTouchMode="true"> 
</AutoCompleteTextView>

并以编程方式设置焦点

((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();

feonResumeonWindowChanged

public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    if (hasFocus) {
        ((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();
    }
}
于 2013-11-09T15:20:44.620 回答
7

以上都不适合我......这就是我使用的

txtView.getParent().requestChildFocus(txtView,txtView);
于 2016-09-11T01:30:29.193 回答
6

解决了!在清单中,我在活动中添加了以下内容:

android:windowSoftInputMode="stateAlwaysVisible"
于 2013-11-09T23:00:35.960 回答
3

You can try this in the code if you want to forcefully show the keyboard.

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);

then you can to use this code to close the keyboard:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autocomplete_zone.getWindowToken(), 0);
于 2013-11-09T15:03:35.613 回答
3

要在代码中执行此操作,请在您的Activity

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
于 2015-09-18T09:39:13.270 回答