Pages

Ads 468x60px

Thursday, September 25, 2014

Android: Show Input Dialog




This code simply shows how to use an input dialog box and get input value from it.


public void showInputDialog(Context context){
AlertDialog.Builder alert = new AlertDialog.Builder(context);

alert.setTitle("Input");
alert.setMessage("Enter Text");

// Set an EditText view to get user input
final EditText input = new EditText(context);
input.setInputType(InputType.TYPE_CLASS_TEXT);
alert.setView(input);

alert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
String value = input.getText().toString().trim();
System.out.println(value);
}
});

alert.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Canceled.
}
});

alert.show();
}

No comments:

Post a Comment