看了SDK,写了这几个例子。
代码如下:
package com.chaowen;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
publicclass DialogActivity extends Activity {
private Button dialog1,dialog2,dialog3,dialog4,dialog5,dialog6,dialog7;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialog1 = (Button)findViewById(R.id.button01);
dialog2 = (Button)findViewById(R.id.button02);
dialog3 = (Button)findViewById(R.id.button03);
dialog4 = (Button)findViewById(R.id.button04);
dialog5 = (Button)findViewById(R.id.button05);
dialog6 = (Button)findViewById(R.id.button06);
dialog7 = (Button)findViewById(R.id.button07);
dialog1.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
AlertDialog.Builder builder =new AlertDialog.Builder(DialogActivity.this);
builder.setMessage("这是一个简单的对话框")
.setCancelable(false)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你按了确定", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你按了取消", Toast.LENGTH_LONG).show();
}
});
AlertDialog alert=builder.create();
alert.show();
}
});
dialog2.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
final CharSequence[] items = {"Red","Green","Blue"};
AlertDialog.Builder builder =new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("选择颜色");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
dialog3.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
final CharSequence[] items = {"Red","Green","Blue"};
AlertDialog.Builder builder =new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("选择颜色");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
dialog4.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
ProgressDialog dialog = ProgressDialog.show(DialogActivity.this, "这是一个圆形处理条",
"Loading. Please wait...", true);
}
});
dialog5.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
ProgressDialog pd =new ProgressDialog(DialogActivity.this);
pd.setTitle("这是一个水平进度条");
pd.setMessage("Load.....");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(true);
pd.show();
}
});
//自定义处理条一
dialog6.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = DialogActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder =new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
});
//自定义处理条二
dialog7.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
Context mContext = DialogActivity.this;
Dialog dialog =new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
}
});
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
publicclass DialogActivity extends Activity {
private Button dialog1,dialog2,dialog3,dialog4,dialog5,dialog6,dialog7;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialog1 = (Button)findViewById(R.id.button01);
dialog2 = (Button)findViewById(R.id.button02);
dialog3 = (Button)findViewById(R.id.button03);
dialog4 = (Button)findViewById(R.id.button04);
dialog5 = (Button)findViewById(R.id.button05);
dialog6 = (Button)findViewById(R.id.button06);
dialog7 = (Button)findViewById(R.id.button07);
dialog1.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
AlertDialog.Builder builder =new AlertDialog.Builder(DialogActivity.this);
builder.setMessage("这是一个简单的对话框")
.setCancelable(false)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你按了确定", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int which) {
Toast.makeText(DialogActivity.this, "你按了取消", Toast.LENGTH_LONG).show();
}
});
AlertDialog alert=builder.create();
alert.show();
}
});
dialog2.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
final CharSequence[] items = {"Red","Green","Blue"};
AlertDialog.Builder builder =new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("选择颜色");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
dialog3.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
final CharSequence[] items = {"Red","Green","Blue"};
AlertDialog.Builder builder =new AlertDialog.Builder(DialogActivity.this);
builder.setTitle("选择颜色");
builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
@Override
publicvoid onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
dialog4.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
ProgressDialog dialog = ProgressDialog.show(DialogActivity.this, "这是一个圆形处理条",
"Loading. Please wait...", true);
}
});
dialog5.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
ProgressDialog pd =new ProgressDialog(DialogActivity.this);
pd.setTitle("这是一个水平进度条");
pd.setMessage("Load.....");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(true);
pd.show();
}
});
//自定义处理条一
dialog6.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = DialogActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
builder =new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
});
//自定义处理条二
dialog7.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
Context mContext = DialogActivity.this;
Dialog dialog =new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
}
});
}
}
源码下载: