I have some TextEdit
inside a LinearLayout
and I want to remove the cursor after I hit enter on them.
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.
Also as it might change I'm using Material3 as a theme:
<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). 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.
CodePudding user response:
If android:imeOptions="actionDone"
doesn't work, you can call setOnEditorActionListener()
on the EditText view:
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int act, KeyEvent event) {
if(act == EditorInfo.IME_ACTION_DONE){
editText.clearFocus();
}
return false;
}
});
CodePudding user response:
Need to add one more thing to the answer after clear focus 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;
}
});