Android:如何讓鍵盤輸入按鈕說「搜索」並處理其點擊?

我沒法弄清楚這一點。 有些應用程序有一個EditText(文本框),當你觸摸它並調出屏幕鍵盤時,鍵盤上有一個「搜索」按鈕而不是一個回車鍵。 java

我想實現這個。 如何實現「搜索」按鈕並檢測按「搜索」按鈕? android

編輯 :找到如何實現搜索按鈕; 在XML中, android:imeOptions="actionSearch"或在Java中, EditTextSample.setImeOptions(EditorInfo.IME_ACTION_SEARCH); 。 可是如何處理用戶按下「搜索」按鈕? 它與android:imeActionId有什麼關係android:imeActionId編輯器


#1樓

在佈局中設置要搜索的輸入法選項。 ide

<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;
    }
});

#2樓

xml文件中, imeOptions="actionSearch"inputType="text"maxLines="1"spa

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

#3樓

用戶單擊搜索時隱藏鍵盤。 除了Robby Pond回答 code

private void performSearch() {
    editText.clearFocus();
    InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    in.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
    //...perform search
}

#4樓

在科特林 orm

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

部分Xml代碼 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>

#5樓

經過XML: get

<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);
相關文章
相關標籤/搜索