Android游戏开发系统控件-Dialog
Android游戏开发系统控件-Dialog
Dialog(对话框)在Android应用开发中经常用到,下面是学习《Android游戏编程从零开始》一书,关于Dialog的初步学习。
创建项目:DialogProject
作者:com_xp
日期:2012/5/13
功能:显示有TextView和按钮的对话框
简单对话框:
添加单选框的对话框:
添加多选框的对话框:
添加列表的对话框:
添加自定义布局的对话框:
项目源代码:
=>>main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello" />
- </LinearLayout>
java进阶 http://www.javady.com/index.php/122.html
=>>dialogmain.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- android:background="#ffffffff"
- android:orientation="horizontal"
- android:id="@+id/myLayout"
- >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="TextView"/>
- <EditText
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="btn1"
- />
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="btn2"
- />
- </LinearLayout>
=>>DialogProjectActivity.java
- package com.dialog;
- import android.app.Activity;
- import android.app.AlertDialog.Builder;
- 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;
- public class DialogProjectActivity extends Activity {
- private Builder builder; //声明Bulider对象
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //实例化Builder对象
- builder = new Builder(DialogProjectActivity.this);
- //设置对话框的图标
- builder.setIcon(android.R.drawable.ic_dialog_info);
- //设置对话框的标题
- builder.setTitle("Dialog");
- //设置对话框提示文本
- // builder.setMessage("Dialog对话框");
- //监听左侧按钮
- builder.setPositiveButton("Yes", new OnClickListener(){
- public void onClick(DialogInterface dialog,int which){
- }
- });
- //监听右侧按钮
- builder.setNegativeButton("No",new OnClickListener(){
- public void onClick(DialogInterface dialog,int which){
- }
- });
- /*
- //添加单选按钮
- builder.setSingleChoiceItems(new String[]{"单选","单选"},1,new
- OnClickListener(){
- public void onClick(DialogInterface dialog,int which){
- //which:选中下标
- }
- });
- //添加复选框
- builder.setMultiChoiceItems(new String[]{"多选","多选"},
- new boolean[]{false, true},new OnMultiChoiceClickListener(){
- public void onClick(DialogInterface dialog,int which,boolean isChecked){
- //which:选中下标
- //isChecked:选中项的勾选状态
- }
- });
- //添加列表项
- builder.setItems(new String[]{"列表项1","列表项2","列表项3"},
- new OnClickListener(){
- public void onClick(DialogInterface dialog,int which){
- //which:选中下标项
- }
- });
- */
- //实例layout布局
- LayoutInflater inflater = getLayoutInflater();
- View layout = inflater.inflate(R.layout.dialogmain, (ViewGroup)findViewById(R.id.myLayout));
- builder.setView(layout);
- //调用show()方法显示对话框
- builder.show();
- }
- }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)