【读书笔记《Android游戏编程之从零开始》】7.Android 游戏开发常用的系统控件(Dialog)
在Android应用开发中,Dialog(对话框)创建简单且易于管理因而经常用到,对话框默认样式类似创建样式的Activity。
首先介绍android.app.AlertDialog下的Builder这个类。Builder是AlertDialog类的子类,而且还是它的内部类。正如其名所示,Builder相当于一个具体的构造者,通过Builder设置对话框属性,然后将Builder(对话框)显示出来。
本人做了个Dialog显示效果集合的小Demo,效果如下(GIF图片较大,需要点加载时间):
主要代码:
package yc.example.dialogshow; import android.app.Activity; import android.app.AlertDialog; import android.app.Dialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; public class MainActivity extends Activity { private static AlertDialog.Builder builder; private static Dialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void Btn1_Onclick(View view) { // 实例化Builder builder = new AlertDialog.Builder(MainActivity.this); // 设置对话框的图标 // builder.setIcon(R.drawable.ic_launcher); builder.setIcon(android.R.drawable.ic_dialog_info); // 设置对话框的图标 builder.setTitle("对话框标题"); // 设置对话框的提示文本 builder.setMessage("对话框的提示文本"); builder.setPositiveButton("确定", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(MainActivity.this, "这是一个简单的对话框", Toast.LENGTH_SHORT).show(); } }); // 调用show()方法显示对话框 builder.show(); } public void Btn2_Onclick(View view) { dialog = new AlertDialog.Builder(this).setIcon(R.drawable.ic_launcher) .setTitle("提示").setMessage("确认退出吗?") .setPositiveButton("确定", null).setNegativeButton("取消", null) .show(); } public void Btn3_Onclick(View view) { dialog = new AlertDialog.Builder(this) .setIcon(android.R.drawable.btn_star).setTitle("喜好调查") .setMessage("您喜欢玩LOL吗?").setPositiveButton("很喜欢", null)// 监听P型按钮,正极性; .setNegativeButton("不喜欢", null)// 监听N型按钮,负极性; .setNeutralButton("一般", null)// 监听中性按钮,一般位于中间 .show(); } public void Btn4_Onclick(View view) { dialog = new AlertDialog.Builder(this) .setIcon(android.R.drawable.ic_dialog_info).setTitle("请输入") /* * setView()方法是往对话框中添加系统组件,但只能设置一个组件,如果多次使用,先设置的组件会被后设置的组件替换掉 * setView()添加一个系统组件的布局,默认都放在对话框的最下方(按钮上面) */ .setView(new EditText(this)).setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); } public void Btn5_Onclick(View view) { final String[] WuJiang = new String[] { "关羽", "张飞", "吕布", "赵云", "马超" }; dialog = new AlertDialog.Builder(this).setTitle("请选择你的初始武将") .setIcon(android.R.drawable.ic_dialog_info) /* * 添加单选框的方法: setSingleChoiceItems(CharSequence[] items, int * checkedItem, OnClickListener listener) items:表示单选的各项文本 * checkedItem:表示单选中默认选中的下标 listener:单击监听器 */ .setSingleChoiceItems(WuJiang, 0, new OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this, "你选择的武将是:" + WuJiang[which], Toast.LENGTH_SHORT) .show(); dialog.dismiss(); } }).show(); } public void Btn6_Onclick(View view) { dialog = new AlertDialog.Builder(this) .setTitle("请选择你喜欢的运动") /* * 添加复选框的方法: setMultiChoiceItems(CharSequence[] items, boolean[] * checkedItems, OnMultiChoiceClickListener listener) * items:表示复选文本的各项文本; checkedItems:表示复选的各项选中状态; listener:多选单击监听器 */ .setMultiChoiceItems( new String[] { "篮球", "足球", "游泳", "赛车", "跑步" }, null, null).setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); } public void Btn7_Onclick(View view) { dialog = new AlertDialog.Builder(this) .setTitle("武将列表") .setItems(new String[] { "吕奉先", "赵子龙", "恶来典韦", "关云长", "张翼德" }, null).setPositiveButton("确定", null) .setNegativeButton("取消", null).show(); } public void Btn8_Onclick(View view) { ImageView img = new ImageView(this); img.setImageResource(R.drawable.goodby_times); dialog = new AlertDialog.Builder(this).setTitle("再见,时光!").setView(img) .setPositiveButton("确定", null).show(); } public void Btn9_Onclick(View view) { // 实例化Layout布局 LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.dialog1, (ViewGroup) findViewById(R.id.dialog_layout)); Button btn_Login = (Button) layout.findViewById(R.id.btn_Login); final EditText uname = (EditText) layout.findViewById(R.id.uname); final EditText pwd = (EditText) layout.findViewById(R.id.pwd); btn_Login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this, "用户名:"+uname.getText()+"\n密码:"+pwd.getText(), Toast.LENGTH_SHORT).show(); dialog.dismiss(); } }); dialog = new AlertDialog.Builder(this).setTitle("用户登录").setView(layout) .show(); } // 进度读取框需要模拟读取 private ProgressDialog progressDialog = null; private final static int MAX_READPROCESS = 100; public void Btn10_Onclick(View view) { progressDialog = new ProgressDialog(MainActivity.this); progressDialog.setIcon(android.R.drawable.ic_dialog_info); progressDialog.setProgress(0); progressDialog.setTitle("程序下载中..."); progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressDialog.setMax(MAX_READPROCESS); progressDialog.show(); new Thread(progress).start(); } // 新开启一个线程,循环的累加,一直到100然后在停止 Runnable progress = new Runnable() { @Override public void run() { int Progress = 0; while (Progress < MAX_READPROCESS) { try { Thread.sleep(50); progressDialog.setProgress(++Progress); } catch (InterruptedException e) { e.printStackTrace(); } } //读取完了以后窗口自消失 progressDialog.cancel(); } }; public void Btn11_Onclick(View view) { progressDialog= new ProgressDialog(MainActivity.this); progressDialog.setMessage("正在为你努力加载数据..."); progressDialog.setIndeterminate(true); progressDialog.setCancelable(true); progressDialog.show(); } }
源代码:DialogShow.zip