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.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();  
•     }  
• }  

 

 

作者:com_xp

發佈留言