当前位置:操作系统 > 安卓/Android >>

Android游戏开发系统控件-Dialog

Android游戏开发系统控件-Dialog

Dialog(对话框)在Android应用开发中经常用到,下面是学习《Android游戏编程从零开始》一书,关于Dialog的初步学习。

创建项目:DialogProject

功能:显示有TextView和按钮的对话框

 

简单对话框:

  
添加单选框的对话框:

 
添加多选框的对话框:

 
添加列表的对话框:

 
添加自定义布局的对话框:

 
 
 

项目源代码:
=>>main.xml
[html]
 
• <?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>  
 
=>>dialogmain.xml
[html]
 
• <?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
[java]
 
• package com.dialog;  
•   
• import android.app.Activity;  
• import android.app.AlertDialog.Builder;  
• import android.content.DialogInte易做图ce;  
• import android.content.DialogInte易做图ce.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(DialogInte易做图ce dialog,int which){  
•                   
•             }  
•         });  
•         //监听右侧按钮  
•         builder.setNegativeButton("No",new OnClickListener(){  
•             public void onClick(DialogInte易做图ce dialog,int which){  
•                   
•           &n

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,