411

私はこれを理解することはできません。一部のアプリにはEditText(テキストボックス)があり、タッチすると画面キーボードが表示され、キーボードにはEnterキーの代わりに[検索]ボタンがあります。

これを実装したい。その検索ボタンを実装して、検索ボタンの押下を検出するにはどうすればよいですか?

編集:検索ボタンの実装方法を見つけました。XMLandroid:imeOptions="actionSearch"またはJavaではEditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH);。しかし、その検索ボタンを押しているユーザーをどのように処理しますか?それは何か関係がありandroid:imeActionIdますか?

4

6 に答える 6

987

レイアウトで、検索する入力方式オプションを設定します。

<EditText
    android:imeOptions="actionSearch" 
    android:inputType="text" />

Javaで、エディターアクションリスナーを追加します。

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            performSearch();
            return true;
        }
        return false;
    }
});
于 2010-07-08T15:44:26.840 に答える
27

ユーザーが検索をクリックしたときにキーボードを非表示にします。ロビー池の答えへの追加

private void performSearch() {
    editText.clearFocus();
    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    //...perform search
}
于 2017-03-16T08:50:23.447 に答える
10

xmlファイルに入れimeOptions="actionSearch"inputType="text"maxLines="1"

<EditText
    android:id="@+id/search_box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/search"
    android:imeOptions="actionSearch"
    android:inputType="text"
    android:maxLines="1" />
于 2016-11-29T13:15:40.593 に答える
10

Kotlinで

evLoginPassword.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_DONE) {
        doTheLoginWork()
    }
    true
}

部分的なXMLコード

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
       <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"

            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

            <EditText
                android:id="@+id/evLoginUserEmail"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/email"
                android:inputType="textEmailAddress"
                android:textColor="@color/black_54_percent" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:paddingLeft="24dp"
            android:paddingRight="24dp">

            <EditText
                android:id="@+id/evLoginPassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password"
                android:inputType="textPassword"
                android:imeOptions="actionDone"
                android:textColor="@color/black_54_percent" />
        </android.support.design.widget.TextInputLayout>
</LinearLayout>
于 2018-08-28T06:02:03.880 に答える
1

XMLによる:

 <EditText
        android:id="@+id/search_edit"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/search"
        android:imeOptions="actionSearch"
        android:inputType="text" />

Javaによる:

 editText.clearFocus();
    InputMethodManager in = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
于 2019-03-07T04:25:10.873 に答える
1

この答えはTextInputEditText用です:

レイアウトXMLファイルで、入力方式のオプションを必要なタイプに設定します。たとえば、完了しました。

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:imeOptions="actionGo"/>

</com.google.android.material.textfield.TextInputLayout>

同様に、imeOptionsをactionSubmit、actionSearchなどに設定することもできます

Javaで、エディターアクションリスナーを追加します。

TextInputLayout textInputLayout = findViewById(R.id.textInputLayout);

textInputLayout.getEditText().setOnEditorActionListener(new 

    TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_GO) {
                performYourAction();
                return true;
            }
            return false;
        }
    });

kotlinを使用している場合:

textInputLayout.editText.setOnEditorActionListener { _, actionId, _ ->
    if (actionId == EditorInfo.IME_ACTION_GO) {
        performYourAction()
    }
    true
}
于 2019-05-28T06:06:05.333 に答える