EditText: implement addTextChangedListener()

This commit is contained in:
Julian Winkler
2023-09-01 13:03:40 +02:00
parent 071538de02
commit 30b990f60a
3 changed files with 43 additions and 0 deletions

View File

@@ -215,6 +215,14 @@ JNIEXPORT jlong JNICALL Java_android_widget_EditText_native_1constructor
JNIEXPORT jstring JNICALL Java_android_widget_EditText_native_1getText
(JNIEnv *, jobject, jlong);
/*
* Class: android_widget_EditText
* Method: native_addTextChangedListener
* Signature: (JLandroid/text/TextWatcher;)V
*/
JNIEXPORT void JNICALL Java_android_widget_EditText_native_1addTextChangedListener
(JNIEnv *, jobject, jlong, jobject);
#ifdef __cplusplus
}
#endif

View File

@@ -21,3 +21,31 @@ JNIEXPORT jstring JNICALL Java_android_widget_EditText_native_1getText(JNIEnv *e
const char *text = gtk_entry_buffer_get_text(gtk_entry_get_buffer(entry));
return _JSTRING(text);
}
struct changed_callback_data {
jobject this;
jobject listener;
jmethodID listener_method;
jmethodID getText;
};
static void changed_cb(GtkEditable* self, struct changed_callback_data *d) {
JNIEnv *env = get_jni_env();
jobject text = (*env)->CallObjectMethod(env, d->this, d->getText);
(*env)->CallVoidMethod(env, d->listener, d->listener_method, text);
if((*env)->ExceptionCheck(env))
(*env)->ExceptionDescribe(env);
}
JNIEXPORT void JNICALL Java_android_widget_EditText_native_1addTextChangedListener(JNIEnv *env, jobject this, jlong widget_ptr, jobject listener) {
GtkEntry *entry = GTK_ENTRY(_PTR(widget_ptr));
struct changed_callback_data *callback_data = malloc(sizeof(struct changed_callback_data));
callback_data->this = _REF(this);
callback_data->listener = _REF(listener);
callback_data->listener_method = _METHOD(_CLASS(listener), "afterTextChanged", "(Landroid/text/Editable;)V");
callback_data->getText = _METHOD(_CLASS(this), "getText", "()Landroid/text/Editable;");
g_signal_connect(GTK_EDITABLE(entry), "changed", G_CALLBACK(changed_cb), callback_data);
}

View File

@@ -2,6 +2,7 @@ package android.widget;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
public class EditText extends TextView {
@@ -16,6 +17,7 @@ public class EditText extends TextView {
@Override
protected native long native_constructor(Context context, AttributeSet attrs);
protected native String native_getText(long widget);
protected native void native_addTextChangedListener(long widget, TextWatcher watcher);
public Editable getText() {
return new FIXME_Editable(native_getText(widget));
@@ -58,4 +60,9 @@ public class EditText extends TextView {
}
}
@Override
public void addTextChangedListener(TextWatcher watcher) {
native_addTextChangedListener(widget, watcher);
}
}