1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-05 12:10:55 +00:00

[Android] Implement native input dialog support

This commit is contained in:
Anish Mishra
2024-10-27 16:29:55 +05:30
parent 08f9cba0fb
commit be5d7f757d
11 changed files with 87 additions and 3 deletions

View File

@@ -1,4 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_edit_height">48dp</dimen>
<dimen name="input_dialog_padding_horizontal">10dp</dimen>
<dimen name="input_dialog_padding_vertical">5dp</dimen>
</resources>

View File

@@ -55,4 +55,7 @@
<string name="kilobytes_per_second">%1$s KB/s</string>
<string name="time_remaining">Time remaining: %1$s</string>
<string name="time_remaining_notification">%1$s left</string>
<!-- Labels for the dialog action buttons -->
<string name="dialog_ok">OK</string>
</resources>

View File

@@ -44,6 +44,7 @@ import android.os.*
import android.util.Log
import android.util.TypedValue
import android.view.*
import android.widget.EditText
import android.widget.FrameLayout
import androidx.annotation.Keep
import androidx.annotation.StringRes
@@ -81,6 +82,7 @@ import java.util.*
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicReference
/**
* Core component used to interface with the native layer of the engine.
*
@@ -772,7 +774,7 @@ class Godot(private val context: Context) {
val builder = AlertDialog.Builder(activity)
builder.setMessage(message).setTitle(title)
builder.setPositiveButton(
"OK"
R.string.dialog_ok
) { dialog: DialogInterface, id: Int ->
okCallback?.run()
dialog.cancel()
@@ -876,6 +878,31 @@ class Godot(private val context: Context) {
mClipboard.setPrimaryClip(clip)
}
/**
* Popup a dialog to input text.
*/
@Keep
private fun showInputDialog(title: String, message: String, existingText: String) {
val activity: Activity = getActivity() ?: return
val inputField = EditText(activity)
val paddingHorizontal = activity.resources.getDimensionPixelSize(R.dimen.input_dialog_padding_horizontal)
val paddingVertical = activity.resources.getDimensionPixelSize(R.dimen.input_dialog_padding_vertical)
inputField.setPadding(paddingHorizontal, paddingVertical, paddingHorizontal, paddingVertical)
inputField.setText(existingText)
runOnUiThread {
val builder = AlertDialog.Builder(activity)
builder.setMessage(message).setTitle(title).setView(inputField)
builder.setPositiveButton(R.string.dialog_ok) {
dialog: DialogInterface, id: Int ->
GodotLib.inputDialogCallback(inputField.text.toString())
dialog.dismiss()
}
val dialog = builder.create()
dialog.show()
}
}
/**
* Destroys the Godot Engine and kill the process it's running in.
*/

View File

@@ -224,6 +224,11 @@ public class GodotLib {
*/
public static native void onNightModeChanged();
/**
* Invoked on the input dialog submitted.
*/
public static native void inputDialogCallback(String p_text);
/**
* Invoked on the GL thread to configure the height of the virtual keyboard.
*/