requestFocus无法正常工作

时间:2013-11-06 19:18:27

标签: android android-edittext textview

<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>

在上面的标记中

<requestFocus />

不起作用。活动开始时,文本视图不会聚焦。这是因为覆盖文本视图的进度条吗?任何想法如何解决这个问题?

5 个答案:

答案 0 :(得分:14)

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

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

并以编程方式设置焦点

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

f.e。在onResumeonWindowChanged

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

    if (hasFocus) {
        ((AutoCompleteTextView) findViewById(R.autocomplete_zone)).requestFocus();
    }
}

答案 1 :(得分:4)

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

android:windowSoftInputMode="stateAlwaysVisible"

答案 2 :(得分:4)

以上都不适合我......这就是我用过的东西

txtView.getParent().requestChildFocus(txtView,txtView);

答案 3 :(得分:2)

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

中执行此操作
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

答案 4 :(得分:1)

如果您想强制显示键盘,可以在代码中尝试此操作。

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

然后您可以使用此代码关闭键盘:

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(autocomplete_zone.getWindowToken(), 0);
相关问题