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