Android游戏开发系统控件-Dialog

Android游戏开发系统控件-Dialog

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

创建项目:DialogProject

作者:com_xp

日期:2012/5/13

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

 

 

简单对话框:

添加单选框的对话框:

添加多选框的对话框:

添加列表的对话框:

 

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

 

项目源代码:

=>>main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="@string/hello" />  
  11.   
  12. </LinearLayout>  

 java进阶   http://www.javady.com/index.php/122.html

=>>dialogmain.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_height="wrap_content"  
  4.     android:layout_width="wrap_content"  
  5.     android:background="#ffffffff"   
  6.     android:orientation="horizontal"  
  7.     android:id="@+id/myLayout"  
  8.     >  
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:text="TextView"/>  
  13.     <EditText   
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content"  
  16.         />  
  17.     <Button   
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content"  
  20.         android:text="btn1"  
  21.         />  
  22.     <Button  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:text="btn2"  
  26.         />     
  27. </LinearLayout>  

 

 

 

=>>DialogProjectActivity.java

  1. package com.dialog;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.AlertDialog.Builder;  
  5. import android.content.DialogInterface;  
  6. import android.content.DialogInterface.OnClickListener;  
  7. import android.os.Bundle;  
  8. import android.view.LayoutInflater;  
  9. import android.view.View;  
  10. import android.view.ViewGroup;  
  11.   
  12. public class DialogProjectActivity extends Activity {  
  13.     private Builder builder; //声明Bulider对象  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         //实例化Builder对象  
  20.         builder = new Builder(DialogProjectActivity.this);  
  21.         //设置对话框的图标  
  22.         builder.setIcon(android.R.drawable.ic_dialog_info);  
  23.         //设置对话框的标题  
  24.         builder.setTitle("Dialog");  
  25.         //设置对话框提示文本  
  26.        // builder.setMessage("Dialog对话框");  
  27.         //监听左侧按钮  
  28.         builder.setPositiveButton("Yes"new OnClickListener(){  
  29.             public void onClick(DialogInterface dialog,int which){  
  30.                   
  31.             }  
  32.         });  
  33.         //监听右侧按钮  
  34.         builder.setNegativeButton("No",new OnClickListener(){  
  35.             public void onClick(DialogInterface dialog,int which){  
  36.                   
  37.             }  
  38.         });  
  39.       /* 
  40.         //添加单选按钮 
  41.         builder.setSingleChoiceItems(new String[]{"单选","单选"},1,new 
  42.                 OnClickListener(){ 
  43.             public void onClick(DialogInterface dialog,int which){ 
  44.                 //which:选中下标 
  45.             } 
  46.         }); 
  47.          
  48.         //添加复选框 
  49.         builder.setMultiChoiceItems(new String[]{"多选","多选"}, 
  50.                 new boolean[]{false, true},new OnMultiChoiceClickListener(){ 
  51.             public void onClick(DialogInterface dialog,int which,boolean isChecked){ 
  52.                 //which:选中下标 
  53.                 //isChecked:选中项的勾选状态 
  54.             } 
  55.         }); 
  56.          
  57.         //添加列表项 
  58.         builder.setItems(new String[]{"列表项1","列表项2","列表项3"}, 
  59.                 new OnClickListener(){ 
  60.             public void onClick(DialogInterface dialog,int which){ 
  61.                 //which:选中下标项 
  62.             } 
  63.         }); 
  64.         */  
  65.         //实例layout布局  
  66.         LayoutInflater inflater = getLayoutInflater();  
  67.         View layout = inflater.inflate(R.layout.dialogmain, (ViewGroup)findViewById(R.id.myLayout));  
  68.         builder.setView(layout);  
  69.         //调用show()方法显示对话框  
  70.         builder.show();  
  71.     }  
  72. }  
posted @ 2012-05-13 13:13  jlins  阅读(1033)  评论(0编辑  收藏  举报