[postlink]https://iamdaowner.blogspot.com/2012/06/android-alertdialog-example.html[/postlink]Hi guys! Today we're going to do an Android AlertDialog script. AlertDialog is a small window that appears or pop up in front of your application. It gets the screen focus and able to accept user interaction. Dialogs are normally used for notifications that should interrupt the user and to perform short tasks.
The preview of this code is this:
References:
http://developer.android.com/reference/android/app/AlertDialog.html
http://developer.android.com/guide/topics/ui/dialogs.html
Android is asking you something. :)) |
- Display an AlertDialog which asks the user if he want to exit the application.
- If the user touched "YES", the program will end.
- If the user touched "NO", the AlertDialog will gone and a toast message will appearsaying "You touched CANCEL".
package com.example.AndroidAlertTutorial;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.widget.Toast;
public class AndroidAlertTutorial extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
PopIt("Exit Application", "Are you sure you want to exit?");
}
public void PopIt( String title, String message ){
new AlertDialog.Builder(this)
.setTitle( title )
.setMessage( message )
.setPositiveButton("YES", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of YES
finish();
}
})
.setNegativeButton("NO", new OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
//do stuff onclick of CANCEL
Toast.makeText(getBaseContext(), "You touched CANCEL", Toast.LENGTH_SHORT).show();
}
}).show();
}
}
The preview of this code is this:
Click to enlarge. |
http://developer.android.com/reference/android/app/AlertDialog.html
http://developer.android.com/guide/topics/ui/dialogs.html
0 comments:
Post a Comment