How to input user texts?

Monkey Targets Forums/Android/How to input user texts?

Midimaster(Posted 2013) [#1]
As I have problems with GetChar() I want to ask you all, how do you call for user's text inputs like players name, etc in your games?

What is the best way? Howdo you do it?


Paul - Taiphoz(Posted 2013) [#2]
I wrote my own little keyboard system for it, I wanted consistency through all the targets which you don't get when targeting different devices.

My code is rough and in no way perfect but it works, it can be seen in my Bit-Invaders game.


Volker(Posted 2013) [#3]
http://www.monkeycoder.co.nz/Community/posts.php?topic=5287

"Shows a native input dialog under XNA, Android, Html5"


Gerry Quinn(Posted 2013) [#4]
Diddy has a function that opens a dialog box.


Midimaster(Posted 2013) [#5]
Thank you all three,

now I did a mixture of your ideas:
MyJava.java:
import android.app.AlertDialog;
import android.widget.EditText;
import android.content.DialogInterface;

class SystemAndroid {
	public static AlertDialog.Builder alert;
	public static EditText input;
	public static String inputString = "";
	public static Boolean _InputIsVisible=false;
   
        static void showEditDialog(String value)
        {
               alert = new AlertDialog.Builder(BBAndroidGame._androidGame._activity);
                input = new EditText(BBAndroidGame._androidGame._activity);
		input.setText(value);
		input.setSingleLine();
                  alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int whichButton) {
                                inputString = input.getText().toString();
                                _InputIsVisible=false;
                        }
                });
               alert.setView(input);
               _InputIsVisible=true;
               alert.show();
        }

       
        static String getInputString()
        {
                return inputString;
        }
        
        static Boolean inputIsVisible() {
		return _InputIsVisible;
	}

}


monkey:
Strict
Import mojo
Import "MyJava.java"

Extern
	Function ShowEditDialog:Void(value:String) = "SystemAndroid.showEditDialog"
	Function GetInputString:String()="SystemAndroid.getInputString"
	Function InputIsVisible:Bool()="SystemAndroid.inputIsVisible"
Public


Class Game Extends App

	Field Input$="", WasVisible%=False

	Method OnCreate%()
		SetUpdateRate 10
		Return 0
	End	


	Method OnUpdate%()
		If KeyHit(KEY_ESCAPE) OnBack()
		If TouchHit(0)
			 ShowEditDialog Input
		Endif
		If WasVisible=True And InputIsVisible()=False
			Input=GetInputString()
		Elseif InputIsVisible()=True
			WasVisible=True
		Endif
		Return 0
	End	


	Method OnRender%()
		Cls
		Scale 2,2
		DrawText "V 09   " + Millisecs(),0,30
		DrawText "java!" + GetInputString() +"!",0,60
		DrawText "monk!" + Input +"!",0,90
		Return 0
	End	
	
	
	Method OnBack%()
		EndApp()
		Return 0
	End
End	

Function Main%()
	New Game
	Return 0
End


*** EDIT ***
With the line "input.setSingleLine();" you will get only one line of input and a RETURN on the keyboard will now already finish the input.


Gerry Quinn(Posted 2013) [#6]
I think you would have to put that into the native code. Even if you don't know the target language it shouldn't be too hard to hack it in.

Note that ShowAlertDialog will suspend your app. If you reload graphics or anything during OnResume, it would probably be best to set a flag to note that you are just suspending it for a few seconds for a dialog, and there is no reason to do anything in OnResume.


Midimaster(Posted 2013) [#7]
With an additional code line "input.setSingleLine();" I now get only one line of input. And a RETURN on the keyboard will now already finish the input.

One last step remains: With the way of using an AlertBox() the user has to do two things to return back to my app: pressing RETURN on the keyboard, then touching OK-button in the AlertBox. Now I want to reach, that already a RETURN on the keyboard closes the Alertbox too. Keep on investigating...