Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created December 11, 2009 06:30
Show Gist options
  • Select an option

  • Save shaobin0604/254032 to your computer and use it in GitHub Desktop.

Select an option

Save shaobin0604/254032 to your computer and use it in GitHub Desktop.
package cn.yo2.aquarium.alertdialogtest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.text.format.DateFormat;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Main extends Activity {
private static final int TIME_ALERT_DIALOG_1 = 1;
private static final int TIME_ALERT_DIALOG_2 = 2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.btn_show_1);
Button button2 = (Button) findViewById(R.id.btn_show_2);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(TIME_ALERT_DIALOG_1);
}
});
button2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(TIME_ALERT_DIALOG_2);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_ALERT_DIALOG_1:
AlertDialog.Builder timeDialog1 = new AlertDialog.Builder(this);
timeDialog1.setTitle("Alert 1: have not called setMessage in onCreateDialog");
return timeDialog1.create();
case TIME_ALERT_DIALOG_2:
AlertDialog.Builder timeDialog2 = new AlertDialog.Builder(this);
timeDialog2.setTitle("Alert 2: have called setMessage in onCreateDialog");
timeDialog2.setMessage("");
return timeDialog2.create();
default:
break;
}
return super.onCreateDialog(id);
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case TIME_ALERT_DIALOG_1:
AlertDialog timeDialog1 = (AlertDialog) dialog;
timeDialog1.setMessage(DateFormat.format("h:mm:ss", System
.currentTimeMillis()));
break;
case TIME_ALERT_DIALOG_2:
AlertDialog timeDialog2 = (AlertDialog) dialog;
timeDialog2.setMessage(DateFormat.format("h:mm:ss", System
.currentTimeMillis()));
break;
default:
break;
}
super.onPrepareDialog(id, dialog);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment