首页 > 代码库 > Android实现“是否退出”对话框和“带图标的列表”对话框

Android实现“是否退出”对话框和“带图标的列表”对话框

今天我们学习的内容是实现两种对话框(Dialog),第一种是询问是否退出对话框,另外一种是带图标的列表对话框,程序的执行效果是,我们点击button1的时候,弹出第一种对话框,我们点击button2的时候,弹出另外一种对话框。

(1)  首先是布局方面,有三个xml文件,一个是Activity的布局文件,一个是Dialog的布局文件,一个是ListView的布局文件,内容分别例如以下:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:orientation="vertical">

    <Button android:id="@+id/exit_button"
       	android:layout_width="wrap_content"
       	android:layout_height="wrap_content"
       	android:layout_gravity="center"
       	android:text="对话框1"/>
    
    <Button android:id="@+id/list_button"
       	android:layout_width="wrap_content"
       	android:layout_height="wrap_content"
       	android:layout_gravity="center"
       	android:text="对话框2"/>

</LinearLayout>

dialog_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:cacheColorHint="#00000000" >
    </ListView>

</LinearLayout>

items.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:maxHeight="72px"
        android:maxWidth="72px"
        android:paddingBottom="20px"
        android:paddingLeft="10px"
        android:paddingTop="20px" />

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10px" />

</LinearLayout>

(2)然后就Java代码部分,代码部分关于dialog我已经具体凝视,具体请看:

package com.example.dialogdemo;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	//定义两个静态字符串
	private static final String DIALOG_ICON = "icon";
	private static final String DIALOG_TITLE = "title";
	
	//声明Button类型的变量button1
	private Button button1;
	
	//声明Button类型的变量button2
	private Button button2;
	
	//声明View类型的变量view
	private View view;
	
	//声明ListView类型的变量listView
	private ListView listView;
	
	//声明一个ArrayList变量mList,它里面的item都是HashMap类型
	private ArrayList<HashMap<String, Object>> mList;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        initData();
        setupViews();
    }
    
    /**
     * 初始化数据
     */
    private void initData(){
    	//初始化mList
    	if(mList == null){
    		mList = new ArrayList<HashMap<String,Object>>();
    	}else{
    		mList.clear();
    	}
    	
    	//向mList里面加入数据
    	HashMap<String, Object> map  = new HashMap<String, Object>();
    	map.put(DIALOG_ICON, R.drawable.img01);
    	map.put(DIALOG_TITLE, "程序管理");
    	mList.add(map);
    	
    	map = new HashMap<String, Object>();
    	map.put(DIALOG_ICON, R.drawable.img02);
    	map.put(DIALOG_TITLE, "保密设置");
    	mList.add(map);
    	
    	map = new HashMap<String, Object>();
    	map.put(DIALOG_ICON, R.drawable.img03);
    	map.put(DIALOG_TITLE, "安全设置");
    	mList.add(map);
    	
    	map = new HashMap<String, Object>();
    	map.put(DIALOG_ICON, R.drawable.img04);
    	map.put(DIALOG_TITLE, "邮件设置");
    	mList.add(map);
    	
    	map = new HashMap<String, Object>();
    	map.put(DIALOG_ICON, R.drawable.img05);
    	map.put(DIALOG_TITLE, "铃声设置");
    	mList.add(map);
    }
    
    /**
     * 初始化视图
     */
    private void setupViews(){
    	LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
		
		view = inflater.inflate(R.layout.dialog_list, null);
		listView = (ListView) view.findViewById(R.id.listview);
		
		DialogListViewAdapter adapter = new DialogListViewAdapter();
		
		listView.setAdapter(adapter);
    	
    	//通过findviewbyid()获取Button对象
    	button1 = (Button) findViewById(R.id.exit_button);
    	button2 = (Button) findViewById(R.id.list_button);
    	
    	//为button1设置点击监听器
    	button1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//初始化一个AlertDialog对象
				AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
				
				//为dialog设置图标
				alertDialog.setIcon(getResources().getDrawable(R.drawable.advise));
				
				//为dialog设置标题
				alertDialog.setTitle("退出?");
				
				//为dialog设置主体内容
				alertDialog.setMessage("是否真的要退出当前应用?");
				
				//为dialog设置一个按钮
				alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "不退出", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
				
				//为dialog设置还有一个按钮
				alertDialog.setButton(DialogInterface.BUTTON_POSITIVE, "是的", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						MainActivity.this.finish();
					}
				});
				
				//显示dialog
				alertDialog.show();
			}
		});
    	
    	//为button2设置点击监听器
    	button2.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//初始化一个AlertDialog
				AlertDialog alertDialog = new  AlertDialog.Builder(MainActivity.this).create();
				
				//为alertDialog设置视图
				alertDialog.setView(view);
				
				//显示alertDialog
				alertDialog.show();
			}
		});
    }
    
    /**
     *	定义ListView的Adapter,其作用是为ListView视图提供数据
     */
    class DialogListViewAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return mList.size();
		}

		@Override
		public Object getItem(int position) {
			return mList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ListViewItemHolder holder;
			
			if(convertView == null){
				convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.items, null);
				holder = new ListViewItemHolder();
				holder.imageView = (ImageView) convertView.findViewById(R.id.image);
				holder.textView = (TextView) convertView.findViewById(R.id.title);
				
				convertView.setTag(holder);
			}else{
				holder = (ListViewItemHolder) convertView.getTag();
			}
			
			HashMap<String, Object> map = mList.get(position);
			int iconId = (Integer) map.get(DIALOG_ICON);
			String title = (String) map.get(DIALOG_TITLE);
			holder.imageView.setImageResource(iconId);
			holder.textView.setText(title);
			
			return convertView;
		}
    	
    }
    
    //两个视图控件的一个封装类
    class ListViewItemHolder{
    	ImageView imageView;
    	TextView textView;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

(3)执行一下android project,效果图例如以下:

技术分享

技术分享


最后附上源代码下载地址:

Android对话框(dialog)实例

Android实现“是否退出”对话框和“带图标的列表”对话框