Native Set
extern "C" JNIEXPORT void JNICALL
Java_com_mgg_threadtest_MainActivity_testDialog(
JNIEnv* env,
jobject activity, jobject listener) {
/*jmethodID jmethod_tem;
// AlertDialog.Builder builder = new AlertDialog.Builder(context);
jclass jclass_AlertDialogBuilder = env->FindClass("android/app/AlertDialog$Builder");
jmethod_tem = env->GetMethodID(jclass_AlertDialogBuilder, "<init>", "(Landroid/content/Context;)V");
jobject jobject_AlertDialogBuilder = env->NewObject(jclass_AlertDialogBuilder, jmethod_tem, activity);
// builder.setTitle("text"); public Builder setTitle(java.lang.CharSequence message)
jmethod_tem = env->GetMethodID(jclass_AlertDialogBuilder, "setTitle",
"(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;");
env->CallObjectMethod(jobject_AlertDialogBuilder, jmethod_tem, env->NewStringUTF("标题"));
// builder.setMessage("text"); public Builder setMessage(CharSequence message)
jmethod_tem = env->GetMethodID(jclass_AlertDialogBuilder, "setMessage",
"(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;");
env->CallObjectMethod(jobject_AlertDialogBuilder, jmethod_tem, env->NewStringUTF("提示信息"));
// builder..setPositiveButton("确定", null);
// public Builder setPositiveButton(CharSequence text, final android.content.DialogInterface.OnClickListener listener)
jmethod_tem = env->GetMethodID(jclass_AlertDialogBuilder, "setPositiveButton",
"(Ljava/lang/CharSequence;Landroid/content/DialogInterface$OnClickListener;)Landroid/app/AlertDialog$Builder;");
env->CallObjectMethod(jobject_AlertDialogBuilder, jmethod_tem, env->NewStringUTF("确定"), static_cast<jobject>(listener));
// builder.show(); public AlertDialog show()
jmethod_tem = env->GetMethodID(jclass_AlertDialogBuilder, "show", "()Landroid/app/AlertDialog;");
env->CallObjectMethod(jobject_AlertDialogBuilder, jmethod_tem);*/
QAndroidJniObject dialog = QAndroidJniObject(
"android.app.AlertDialog$Builder",
"(Landroid/content/Context;)V",
activity);
dialog.callObjectMethod(
"setMessage",
"(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;",
env->NewStringUTF("提示信息")
);
dialog.callObjectMethod(
"setTitle",
"(Ljava/lang/CharSequence;)Landroid/app/AlertDialog$Builder;",
env->NewStringUTF("标题")
);
dialog.callObjectMethod(
"setNegativeButton",
"(Ljava/lang/CharSequence;Landroid/content/DialogInterface$OnClickListener;)Landroid/app/AlertDialog$Builder;",
env->NewStringUTF("取消"),
// static_cast<jobject>(listener)
[=](){
__android_log_print(ANDROID_LOG_ERROR, "test", "Test setNegativeButton");
return static_cast<jobject>(listener);
}
);
dialog.callObjectMethod(
"setPositiveButton",
"(Ljava/lang/CharSequence;Landroid/content/DialogInterface$OnClickListener;)Landroid/app/AlertDialog$Builder;",
env->NewStringUTF("确定"),
static_cast<jobject>(listener)
);
dialog.callObjectMethod(
"show",
"()Landroid/app/AlertDialog;"
);
}
Kotlin Set
external fun testDialog(param: DialogInterface.OnClickListener)
binding.tvNativeDialog.setOnClickListener {
testDialog { dialog, which ->
when (which) {
BUTTON_POSITIVE -> {
Toast.makeText(asActivity(),"确认被点击", Toast.LENGTH_SHORT).show()
}
BUTTON_NEGATIVE -> {
Toast.makeText(asActivity(),"取消被点击", Toast.LENGTH_SHORT).show()
}
else -> {
}
}
Timber.e("DialogInterface.OnClickListener:${which} $dialog")
}
}
网友评论