Android游戏开发系统控件-Dialog

Android游戏开发系统控件-Dialog

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

创建项目:DialogProject

作者:wwj

日期: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>


 

 

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


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

posted on 2012-05-13 12:18  1.曲待续  阅读(154)  评论(0编辑  收藏  举报

导航