Request Text Input


This is an example taken from my new app (App2System PRO) of how a dialog can be used to request user input.

These strings need to be announsed at the start of the Activity to allow the to be used throughout it.

String value;
String New_Value;

The rest of this, being a dialog, can be put in the onCreate bundle or in a public void.

AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle(“Title”);
alert.setMessage(“Message”);

// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton(“Ok”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
value = input.getText().toString();
New_Value = value.replaceAll(” “, “_”);//this is an example of how strings can be eddited at run time.
if (New_Value.equals(“”)) {

//here you can put what you want done if the text field was left empty.
Intent i = new Intent(Main2.this, Main1.class);
startActivity(i);
finish();
Toast msg1 = Toast.makeText(Install_System.this,
“Please Enter Some Text”, Toast.LENGTH_LONG);
msg1.show();
} else {
//do something with the string/text

Toast msg1 = Toast.makeText(Install_System.this,
“was ” + value “, now ” + New_Value, Toast.LENGTH_LONG);
msg1.show();
}
}
});
alert.setNegativeButton(“Cancel”, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Intent i = new Intent(main2.this, main1.class);
startActivity(i);
finish();
}
});
alert.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
Intent i = new Intent(main2.this, main1.class);
startActivity(i);
finish();
}
});

alert.show();
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: