12 Mar

Android Input Dialog

Kadang kita perlu membuat dialog untuk menginputkan data. Misalnya kita meminta inputan nama dari suatu proses. Kita tidak perlu membuat layout baru, kita cukup memanggil kode program berikut:

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) {
String value = input.getText().toString();
// Do something with value!
}
});

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

alert.show();

Leave a Reply

Your email address will not be published. Required fields are marked *