簡體   English   中英

Android:EditText和Button; 單擊按鈕時,取消聚焦EditText並隱藏軟鍵盤?

[英]Android: EditText and Button; when click Button, unfocus EditText and hide soft keyboard?

<EditText android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:layout_weight="1"
    android:id="@+id/name" android:singleLine="true"
    android:maxLength="12" android:capitalize="none" android:inputType="text" />
<Button android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:id="@+id/search_button"
    android:text="Search" />

我在申請的頂部有這個。 當應用程序啟動時,EditText突出顯示為橙色並且其中有一個光標; 點擊EditText時,會彈出軟鍵盤。 用戶使用它鍵入EditText。

但是,當他們單擊Button時,我的onClick方法會觸發並執行它應該執行的所有操作,但是軟鍵盤會停留在屏幕上並且EditText仍會使用其光標突出顯示。

我也有,在按鈕onclick的頂部:

findViewById(R.id.name).clearFocus();

盡管如此,EditText似乎還沒有清楚它的重點。 如何使按鈕實際上就像提交表單一樣?

此外,我不會在點擊按鈕時轉換到其他活動。 我想這是典型的情況,也可能是他們不打擾隱藏鍵盤的原因。 但是我希望將搜索框和按鈕保持在屏幕頂部,這樣我只需在按下按鈕時動態填充並向屏幕添加視圖。

我怎樣才能實現我想要的行為?

你可以通過調用它來隱藏鍵盤。

InputMethodManager imm = (InputMethodManager) getSystemService(
    INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(getWindowToken(), 0);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM