0
0
mirror of https://github.com/TrianguloY/UrlChecker.git synced 2024-09-19 20:02:16 +02:00

place cursor on clicked position and force open/close the keyboard

This commit is contained in:
TrianguloY 2024-06-12 22:45:29 +02:00
parent 7660a6670f
commit 94eabe486e
3 changed files with 35 additions and 12 deletions

View File

@ -1,7 +1,9 @@
package com.trianguloy.urlchecker.modules.list;
import android.content.Context;
import android.text.Editable;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
@ -13,12 +15,11 @@ import com.trianguloy.urlchecker.modules.AModuleData;
import com.trianguloy.urlchecker.modules.AModuleDialog;
import com.trianguloy.urlchecker.modules.DescriptionConfig;
import com.trianguloy.urlchecker.url.UrlData;
import com.trianguloy.urlchecker.utilities.methods.AndroidUtils;
import com.trianguloy.urlchecker.utilities.wrappers.DefaultTextWatcher;
import com.trianguloy.urlchecker.utilities.wrappers.DoubleEvent;
/**
* This module shows the current url and allows manual editing
*/
/** This module shows the current url and allows manual editing */
public class TextInputModule extends AModuleData {
@Override
@ -45,6 +46,7 @@ public class TextInputModule extends AModuleData {
class TextInputDialog extends AModuleDialog {
private final DoubleEvent doubleEdit = new DoubleEvent(1000); // if two updates happens in less than this milliseconds, they are considered as the same
private final InputMethodManager inputMethodManager;
private boolean skipUpdate = false;
private TextView txt_url;
@ -52,6 +54,7 @@ class TextInputDialog extends AModuleDialog {
public TextInputDialog(MainDialog dialog) {
super(dialog);
inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
}
@Override
@ -81,21 +84,28 @@ class TextInputDialog extends AModuleDialog {
}
});
txt_url.setOnClickListener(v -> {
AndroidUtils.setOnClickWithPositionListener(txt_url, position -> {
edtxt_url.setSelection(txt_url.getOffsetForPosition(position.first, position.second));
txt_url.setVisibility(View.GONE);
edtxt_url.setVisibility(View.VISIBLE);
// force open the keyboard
edtxt_url.requestFocus();
inputMethodManager.showSoftInput(edtxt_url, 0);
});
}
@Override
public void onDisplayUrl(UrlData urlData) {
// setText fires the afterTextChanged listener, so we need to skip it
skipUpdate = true;
txt_url.setText(urlData.url);
edtxt_url.setText(urlData.url);
txt_url.setVisibility(View.VISIBLE);
edtxt_url.setVisibility(View.GONE);
// force close the keyboard
inputMethodManager.hideSoftInputFromWindow(edtxt_url.getWindowToken(), 0);
skipUpdate = false;
doubleEdit.reset(); // next user update, even if immediately after, will be considered new
}

View File

@ -11,6 +11,7 @@ import android.os.Build;
import android.text.SpannableStringBuilder;
import android.text.style.ClickableSpan;
import android.util.Log;
import android.util.Pair;
import android.util.Patterns;
import android.util.TypedValue;
import android.view.MenuItem;
@ -213,4 +214,16 @@ public interface AndroidUtils {
StreamUtils.inputStream2File(in, file);
}
}
/** OnClickListener that reports the click position */
static void setOnClickWithPositionListener(View view, JavaUtils.Consumer<Pair<Float, Float>> listener) {
var xy = new float[2];
view.setOnTouchListener((v, event) -> {
// store any interaction and continue
xy[0] = event.getX();
xy[1] = event.getY();
return false;
});
view.setOnClickListener(v -> listener.accept(Pair.create(xy[0], xy[1])));
}
}

View File

@ -1,9 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true">
android:focusableInTouchMode="true"
android:orientation="vertical">
<requestFocus
android:layout_width="0px"
@ -21,6 +21,6 @@
style="@android:style/Widget.EditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"/>
android:textAppearance="@android:style/TextAppearance.DeviceDefault.Large"
android:visibility="gone" />
</LinearLayout>