[英]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.