8

アプリにいくつかの設定ページがあります。アクティビティが直接開始されると、テキストの編集に焦点が当てられ、次のコードを使用してフォーカスをクリアしました。

<RelativeLayout 
     android:id="@+id/RequestFocusLayout"
     android:focusable="true"
     android:focusableInTouchMode="true"
     android:layout_width="0px"
     android:layout_height="0px"/>

およびJavaコード

 RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
 focuslayout.requestFocus();

上記のコードは、アクティビティが最初に開始されたときに正常に機能しており、同じアクティビティが再び開始された場合は、自動的にテキストを編集してフォーカスを取得します。

誰かが私がこの問題を解決するのを手伝ってくれますか?

4

8 に答える 8

13

実際には、アクティビティの最初のフォーカス可能なビューが最初のフォーカスを受け取ります。それがたまたまあなたEditTextのものである場合、それは最初に焦点を合わせられます。

それを望まない場合は、次のオプションがあります。

別のビューに焦点を合わせる

最初に焦点を当てたいビューをプログラムで特定します。

@Override
protected void onStart() {

    super.onStart();
    findViewById( R.id.yourOtherViewId ).requestFocus();
}

レイアウトの以前のビューをフォーカス可能にする

「ビューに最初のフォーカスがない」ように見える場合は、親ビューグループをフォーカス可能にすることができます。次の例では、次のようにLinearLayout設定してフォーカス可能にしますandroid:focusableInTouchMode="true"

<LinearLayout
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <EditText
        ...
于 2013-04-18T19:53:19.157 に答える
9
If Come back from other activity edittext get focused. 

これらの行をonStart()またはonReusme()に配置します

  RelativeLayout focuslayout = (RelativeLayout) findViewById(R.id.RequestFocusLayout);
 focuslayout.requestFocus();
于 2012-06-22T14:12:39.753 に答える
5

このページに表示されているソリューションを使用しましたが、機能しませんでした。この属性をアクティビティタグのAndroidManifestに追加します。

android:windowSoftInputMode="stateHidden"

それは完璧に動作します。

于 2013-03-22T11:16:09.837 に答える
5

EditTextがRelativeLayoutの子である場合android:descendantFocusability、フォーカスを要求するために使用できます。

<RelativeLayout 
     android:id="@+id/RequestFocusLayout"
     android:focusable="true"
     android:layout_width="0px"
     android:layout_height="0px" 
     android:descendantFocusability="beforeDescendants" 
     android:focusableInTouchMode="true" />
于 2012-06-22T15:43:54.943 に答える
4

私は本当にandroid:focusableInTouchMode="true"私の親ビューグループでのみ助けます。

于 2017-10-14T08:06:47.787 に答える
0

onStart()メソッドのフォーカスを削除するコードを配置すると、正常に機能するはずです。

于 2012-06-22T13:45:10.367 に答える
0

レイアウトXMLファイルで、EditTextにimeOptionを指定します。

android:imeOptions="actionGo"

次に、アクティビティのJavaファイルのEditTextにアクションリスナーを追加します

mYourEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_GO) {
            // hide virtual keyboard
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mYourEditText.getWindowToken(), 0);
            return true;
        }
        return false;
    }
});

mYourEditTextはEditTextオブジェクトです。

于 2013-03-22T15:22:18.560 に答える
0

アクティビティを開始すると、画面上のEditTextに焦点が当てられます。使用する

android:focusableInTouchMode="true"

そのEditTextを含む親で。EditTextがRelativeLayout内にある場合と同様に、RelativeLayoutでこの行を使用します。

于 2019-07-18T22:14:08.663 に答える