簡體   English   中英

Android 按下 Enter 時移除 TextEdit 的焦點

[英]Android remove focus of TextEdit when Enter is pressed

我在LinearLayout中有一些TextEdit ,我想在輸入 cursor 后刪除它們。

代碼

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".activity.SettingActivity"
    android:focusableInTouchMode="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/txtInterval1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Edit Text 1"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/etxtInterval1"
        android:layout_width="135dp"
        android:imeOptions="actionDone"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:autofillHints="no"
        android:ems="10"
        android:inputType="numberSigned"
        android:minHeight="48dp"
        android:text="2"
        android:textAlignment="center"
        tools:ignore="LabelFor" />

    <TextView
        android:id="@+id/txtInterval2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Edit Text 2"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/etxtInterval2"
        android:layout_width="135dp"
        android:imeOptions="actionDone"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:autofillHints="no"
        android:ems="10"
        android:inputType="numberSigned"
        android:minHeight="48dp"
        android:text="3"
        android:textAlignment="center"
        tools:ignore="LabelFor" />

    <TextView
        android:id="@+id/txtInterval3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Edit Text 3"
        android:textStyle="bold" />


    <EditText
        android:id="@+id/etxtInterval3"
        android:layout_width="135dp"
        android:imeOptions="actionDone"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:autofillHints="no"
        android:ems="10"
        android:inputType="numberSigned"
        android:minHeight="48dp"
        android:text="3"
        android:textAlignment="center"
        tools:ignore="LabelFor" />

</LinearLayout>

問題

在鍵盤上按 Enter 一段時間后,標記(紅色圓圈)消失,但 cursor 仍然存在。

在此處輸入圖像描述

同樣因為它可能會改變我使用 Material3 作為主題:

<resources xmlns:tools="http://schemas.android.com/tools">
    <style name="AppTheme" parent="Theme.Material3.Light.NoActionBar">
        ...
    </style>
</resources>

如何刪除 cursor 和任何其他提示(按回車后)。 我覺得這讓用戶認為他們仍在編輯該字段,並且在按下回車后應該失去焦點。 我正在考慮使用setOnKeyListener並在那里松散焦點,但我想知道正確的解決方案,因為這似乎是一個很常見的場景。

如果android:imeOptions="actionDone"不起作用,您可以在 EditText 視圖上調用setOnEditorActionListener()

editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int act, KeyEvent event) {
        if(act == EditorInfo.IME_ACTION_DONE){
             editText.clearFocus();
        }
    return false;
    }
});

清除焦點后需要在答案中再添加一件事 hideSoftInputFromWindow Manager

    editText.setOnEditorActionListener(new OnEditorActionListener() {        
    @Override
    public boolean onEditorAction(TextView v, int act, KeyEvent event) {
        if(act == EditorInfo.IME_ACTION_DONE){
             editText.clearFocus();
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    return false;
    }
});

暫無
暫無

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

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