简体   繁体   English

Android 按下 Enter 时移除 TextEdit 的焦点

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

I have some TextEdit inside a LinearLayout and I want to remove the cursor after I hit enter on them.我在LinearLayout中有一些TextEdit ,我想在输入 cursor 后删除它们。

Code代码

<?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>

Problem问题

After some time pressing enter on the keyboard, the marker (circled in red) disappears but the cursor is still there.在键盘上按 Enter 一段时间后,标记(红色圆圈)消失,但 cursor 仍然存在。

在此处输入图像描述

Also as it might change I'm using Material3 as a theme:同样因为它可能会改变我使用 Material3 作为主题:

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

How can I remove the cursor and any other hint (after enter is pressed).如何删除 cursor 和任何其他提示(按回车后)。 I feel this makes the user think they are still editing that field and that focus should be lost after pressing enter.我觉得这让用户认为他们仍在编辑该字段,并且在按下回车后应该失去焦点。 I'm thinking of using setOnKeyListener and loose focus there, but I wanted to know the proper solution to this as it seems a pretty common scenario.我正在考虑使用setOnKeyListener并在那里松散焦点,但我想知道正确的解决方案,因为这似乎是一个很常见的场景。

If android:imeOptions="actionDone" doesn't work, you can call setOnEditorActionListener() on the EditText view:如果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;
    }
});

Need to add one more thing to the answer after clear focus hideSoftInputFromWindow Manager清除焦点后需要在答案中再添加一件事 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.

相关问题 更改edittext的焦点在EnterPressed_android - Change focus of edittext on Enter pressed_android 在Android中按下Enter键时如何触发按钮? - How to Trigger a Button when Enter is Pressed in Android? Android:用于循环处理对TextEdit字段失去关注的问题 - Android: for cycle to handle lost focus on TextEdit fields Android:在用户输入编号时绘制TextEdit或TextView。[exp:6然后绘制6 TextView] - Android : Draw TextEdit(s) or TextView(s) when user enter number of them.[exp: 6 then draw 6 TextView] Eclipse editText在按下完成后删除焦点(光标) - Eclipse editText remove focus (cursor) when done is pressed 按下Enter键时隐藏键盘-Android WebView - Hide the keyboard when enter button is pressed - Android WebView 按下两次时,EditText上的Android onKeyListener仅响应“ Enter”键 - Android onKeyListener on EditText only responds to “Enter” key when pressed twice 在Android上按下主键按钮后,活动将失去焦点 - The activity loses focus when home key button is pressed on Android 为什么在Android中按下背景按钮时没有焦点? - Why Button with Background gets no focus when pressed in android? 在 Android 4.x 上按下时,Listview 中的 EditText 失去焦点 - EditText in Listview loses focus when pressed on Android 4.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM