[英]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 后删除它们。
<?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>
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.