书中这部分展示了4种Android对话框。
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ActivityMain extends Activity {
private static final int DIALOG1 = 1;
private static final int DIALOG2 = 2;
private static final int DIALOG4 = 4;
private static final int DIALOG3 = 3;
@Override
protected Dialog onCreateDialog(int id) {
switch (id) { //根据不同分支生成不同的对话框
case DIALOG1:
return buildDialog1(ActivityMain.this);
case DIALOG2:
return buildDialog2(ActivityMain.this);
case DIALOG3:
return buildDialog3(ActivityMain.this);
case DIALOG4:
return buildDialog4(ActivityMain.this);
}
return null;
}
//显示对话框之前调用的方法
protected void onPrepareDialog(int id, Dialog dialog){
if(id==DIALOG1){
setTitle("测试");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alert_dialog); //设置当前的显示
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG1); //设置单击后的事件
}
});
Button buttons2 = (Button) findViewById(R.id.buttons2);
buttons2.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG2);
}
});
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG3);
}
});
Button button4 = (Button) findViewById(R.id.button4);
button4.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
showDialog(DIALOG4);
}
});
}
private Dialog buildDialog1(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context); //创建器
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_two_buttons_title);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
}); //确定按钮的定义和事件
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});//取消按钮的定义和事件。
return builder.create();
}
private Dialog buildDialog2(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context); //下面都是从配置文件当中取出内容设置对话框属性。
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_two_buttons_msg);
builder.setMessage(R.string.alert_dialog_two_buttons2_msg);
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
builder.setNeutralButton(R.string.alert_dialog_something,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的进入详细按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
private Dialog buildDialog3(Context context) {
LayoutInflater inflater = LayoutInflater.from(this);
final View textEntryView = inflater.inflate(
R.layout.alert_dialog_text_entry, null); //还原配置文件到View对象
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.alert_dialog_icon);
builder.setTitle(R.string.alert_dialog_text_entry);
builder.setView(textEntryView); //设置显示样式
builder.setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的确定按钮");
}
});
builder.setNegativeButton(R.string.alert_dialog_cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setTitle("点击了对话框上的取消按钮");
}
});
return builder.create();
}
//进度条对话框很简单
private Dialog buildDialog4(Context context) {
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("正在下载歌曲");
dialog.setMessage("请稍候……");
return dialog;
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?