Android弹出对话框简单代码
package com.zxing.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInte易做图ce;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class DialogTest extends Activity {
private static final int DIALOG = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(DIALOG);//Activity里面的函数,然后调用onCreateDialog这个方法,最后调用onPrepareDialog这个方法 }
});
}
@Override
protected Dialog onCreateDialog(int id) {
// TODO Auto-generated method stub
if(id==1){
return this.buildDialog(DialogTest.this);
}else{
return null;
}
}
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
// TODO Auto-generated method stub
super.onPrepareDialog(DIALOG, dialog);
}
private Dialog buildDialog(Context context){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alertitle);
builder.setPositiveButton(R.string.right,new DialogInte易做图ce.OnClickListener() {
@Override
public void onClick(DialogInte易做图ce dialog, int which) {
// TODO Auto-generated method stub
setTitle("您单击了对话框上面的正确按钮");
}
});
return builder.create();
}
}
作者:MR.zxing
补充:移动开发 , Android ,