`
1140566087
  • 浏览: 547806 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
博客专栏
2c4ae07c-10c2-3bb0-a106-d91fe0a10f37
c/c++ 入门笔记
浏览量:18077
3161ba8d-c410-3ef9-871c-3e48524c5263
Android 学习笔记
浏览量:309547
Group-logo
J2ME 基础学习课程集
浏览量:18008
A98a97d4-eb03-3faf-af96-c7c28f709feb
Spring 学习过程记录...
浏览量:17196
社区版块
存档分类
最新评论

Android 之 对话框总结

阅读更多
各种对话测试布局文件:

<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/user_dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="自定义对话框测试" />
    <Button
        android:id="@+id/general_list_dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="普通列表对话框测试" />
    <Button
        android:id="@+id/multic_list_dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="多选列表对话框测试" />
    <Button
        android:id="@+id/radio_list_dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="单选列表对话框测试" />
     <Button
        android:id="@+id/adapter_list_dialog"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="提供列表项的列表对话框测试" />
    
  
</LinearLayout>


主程序代码测试:

/*
 * 对话框测试主入口
 * @author Administrator
 *
 */

public class DialogMainActivity extends Activity {

	private RadioGroup color,size;
	private RadioButton c1,c2,c3,c4,c5,s1,s2,s3,s4,s5;
	private TextView textView;
	private Button general,multic,radio,adapter,user;
	
	// 初始化
	public void init(){
		
		user = (Button) findViewById(R.id.user_dialog);
		general = (Button) findViewById(R.id.general_list_dialog);
		multic = (Button) findViewById(R.id.multic_list_dialog);
		radio = (Button) findViewById(R.id.radio_list_dialog);
		adapter = (Button) findViewById(R.id.adapter_list_dialog);
	}

	// 程序入口
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_main_layout);
		init(); // 初始化
		
		user.setOnClickListener(listener);
		general.setOnClickListener(listener);
		multic.setOnClickListener(listener);
		radio.setOnClickListener(listener);
		adapter.setOnClickListener(listener);
	}
	
	/**
	 * 普通列表对话框测试
	 */
	public void generalListDialog(){
		Builder b = new Builder(this);
		b.setTitle("普通列表对话框测试");
		b.setIcon(R.drawable.ic_launcher);
		
		b.setItems(new String[]{"西方","南方","北方","西北方"},new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) { // which 代表被点击的项
				
				Toast.makeText(DialogMainActivity.this, "选中了:"+new String[]{"西方","南方","北方","西北方"}[which],Toast.LENGTH_LONG).show();
			}
		});
		
		b.create();
		b.show();
	}
	
	/**
	 * 单选对话框测试
	 */
	public void radioListDialog(){
		Builder b = new AlertDialog.Builder(this);
		b.setTitle("单选列表对话框测试");
		b.setIcon(R.drawable.ic_launcher);
		final String[] data = new String[]{"男","女","中"};
		/*
		 * 参数: 数据  默认选项  事件
		 */
		b.setSingleChoiceItems(data, 0, new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				
				Toast.makeText(DialogMainActivity.this, "选中了:"+data[which],Toast.LENGTH_LONG).show();
			}
		});
		
		// 确定
		b.setPositiveButton("确定", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		// 取消
		b.setNegativeButton("取消",new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
		
			}
		});
		
		b.create();
		b.show();
	}
	
	/**
	 * 创建多选列表对话框
	 */
	public void multicListDialog(){
		Builder b = new AlertDialog.Builder(this);
		b.setTitle("多选列表对话框测试");
		b.setIcon(R.drawable.ic_launcher);
		final String[] data = new String[]{"吃饭","冲浪","散步"};
		/*
		 * 参数: 数据  默认选项  事件
		 */
		b.setMultiChoiceItems(data, null, new OnMultiChoiceClickListener() {
		
			public void onClick(DialogInterface dialog, int which, boolean isChecked) {
				
				/* which 索引  - isChecked 是否被选中 */
				if(isChecked){
					Toast.makeText(DialogMainActivity.this, "选中了:"+data[which],Toast.LENGTH_LONG).show();
				}
			}
		});
		
		// 确定
		b.setPositiveButton("确定", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		// 取消
		b.setNegativeButton("取消",new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
		
			}
		});
		
		b.create();
		b.show();
	}

	/**
	 * 提供列表项的对话框测试
	 */
	public void adapterListDialog(){
		Builder b = new AlertDialog.Builder(this);
		b.setTitle("提供列表对话框测试");	// 标题
		b.setIcon(R.drawable.ic_launcher); // 图标
	
		/*
		 * 参数: 数据  默认选项  事件
		 */
	
		
		// 确定
		b.setPositiveButton("确定", new OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
			}
		});
		
		// 取消
		b.setNegativeButton("取消",new OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
		
			}
		});
		
		b.create();
		b.show();
	}
	
	// 事件 -- 测试各种动作
	private android.view.View.OnClickListener listener = new View.OnClickListener() {
		
		public void onClick(View v) {
			if(v.getId() == R.id.general_list_dialog){ // 普通测试
				generalListDialog();
				return;
			}
			if(v.getId() == R.id.user_dialog){ // 自定义测试
				userDialog();
				return;
			}
			if(v.getId() == R.id.radio_list_dialog){ // 单选测试
				radioListDialog();
				return;
			}
			if(v.getId() == R.id.multic_list_dialog){ // 多选测试
				multicListDialog();
				return;
			}
			if(v.getId() == R.id.adapter_list_dialog){ // 多选测试
				adapterListDialog();
				return;
			}
			
		}
	};
	
	
	/**
	 * 自定义对话框测试
	 */
	public void userDialog(){

		// 创建对话框对象
		Builder builder = new Builder(this);
		builder.setTitle("字体样式选择"); // 设置标题
		builder.setIcon(R.drawable.ic_launcher); // 设置图标

		// 确定按钮  -- 执行预期动作
		builder.setPositiveButton("确定",new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

			}
		});

		/*  载入自定义布局文件 -- 达成自定义的效果  */
		LayoutInflater inflater = LayoutInflater.from(this); // 获取视图容器对象

		// 根据视图容器获取布局对象
		LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.user_dialog_item,null); 

		builder.setView(layout); // 将布局视图显示在对话框中

		// 获取自定义布局中的组件对象
		color = (RadioGroup) layout.findViewById(R.id.colorRadioGroup);
		size = (RadioGroup) layout.findViewById(R.id.sizeRadioGroup);
		c1 = (RadioButton) layout.findViewById(R.id.c1);
		c2 = (RadioButton) layout.findViewById(R.id.c2);
		c3 = (RadioButton) layout.findViewById(R.id.c3);
		c4 = (RadioButton) layout.findViewById(R.id.c4);
		s1 = (RadioButton) layout.findViewById(R.id.s1);
		s2 = (RadioButton) layout.findViewById(R.id.s2);
		s3 = (RadioButton) layout.findViewById(R.id.s3);
		s4 = (RadioButton) layout.findViewById(R.id.s4);
		
		// 设置监听器
		color.setOnCheckedChangeListener(radioListener);
		size.setOnCheckedChangeListener(radioListener);
		
		/* ------------------------------------------------------------------ */

		// 取消按钮,点击取消对话框
		builder.setNegativeButton("取消",new OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub

			}
		});

		builder.create(); //  创建

		builder.show(); // 显示

	}

	// RadioGroup 事件改变监听器
	private OnCheckedChangeListener radioListener = new OnCheckedChangeListener() {

		public void onCheckedChanged(RadioGroup group, int checkedId) {
			
			if(checkedId == c1.getId()){ // 
				Toast.makeText(DialogMainActivity.this,"字体颜色", 1000).show();
				
			}
			if(checkedId == s1.getId()){
				Toast.makeText(DialogMainActivity.this,"字体大小", 1000).show();
				
			}
		}
	};

}



=============================自定义对话框======================================


自定义对话框布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/user_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:padding="15dp" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="请看这里" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="颜色选择:" />

        <RadioGroup
            android:id="@+id/colorRadioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/c1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <RadioButton
                android:id="@+id/c2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <RadioButton
                android:id="@+id/c3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <RadioButton
                android:id="@+id/c4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="颜色选择:" />

        <RadioGroup
            android:id="@+id/sizeRadioGroup"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/s1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <RadioButton
                android:id="@+id/s2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <RadioButton
                android:id="@+id/s3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <RadioButton
                android:id="@+id/s4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />
        </RadioGroup>
    </LinearLayout>

</LinearLayout>



主程序入口:
分享到:
评论

相关推荐

    android对话框总结

    android对话框总结

    Android对话框的总结(8.15)

    Android对话框的总结(8.15)

    Android自定义对话框

    简单的总结:自定义对话框有如下步骤。 1.自定义对话框样式(R.style.xxx) 2.自定义对话框布局(R.layout.xxx) 3.通过Dialog构建对话框(Dialog.setContentView(R.layout.xxx)) 4.处理事件和业务逻辑(按钮,列表,...

    PromptBox.rar

    这个代码对应Android基础篇-对话框总结(普通对话框,单选框,多选框,自定义对话框)这篇博客上所有代码,免费下载,以调试可以正常运行。

    Android对话框精粹1

    本人总结了常用Android对话框(dialog)案列,借鉴网上的资源,总结出日常所用的经典对话框。适合日常开发借鉴使用,也适合初学者学习。

    Android基础篇-对话框总结(普通对话框,单选对话框,多选对话框,自定义对话框)

    一:AlterDialog对话框 2:对话框圆角显示(在drawable下创建radius_bomb_box.xml) 3:Styles样式设置 @color/colorPrimary @color/colorPrimaryDark ... @android:color/transparent true @null

    android对话框的使用

    本文档总结了android中的对话框的使用以及源代码!希望可以广大的android学习者带来一定的帮助

    Android对话框精粹

    本人总结了常用Android对话框(dialog)案列,借鉴网上的资源,总结出日常所用的经典对话框。适合日常开发借鉴使用,也适合初学者学习。||前期下载过Android对话框精粹1的朋友们,本次是对上次的更新,里面增加了很...

    android对话框

    android对话框使用很广泛,看了很多,总结了一下

    安卓Dialog对话框相关-androiddialog总结Dialog整理.rar

    android dialog总结Dialog整理.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。

    Android开发必知 九种对话框的实现方法

    这里总结了九种对话框的实现方法,有需要的朋友可以来学习下了   除了popupwindow实现稍微麻烦一点,其他形似都相对简单,熟悉2便即可 直接上源码 package com.naoh.stu; import java.util.A

    Android中Dialog对话框的使用小结

    主要给大家总结了一些关于Android中Dialog对话框的使用方法,这其中包括普通对话框、确定取消对话框、多按钮对话框、列表对话框、带Adapter的对话框、单选对话框以及多选对话框等,需要的朋友可以参考学习,下面来...

    自己平时做的android相关的总结

    activity变身对话框 onMeasure 中的AT_MOST EXACTLY UNSPECIFIED MotionEvent的触发记录 对于Drawable 的 getIntrinsicHeight 和getIntrinsicWidth的理解 IntentService 使用总结: 文件读写总结: AES 解密失败: ...

    android知识大总结【邯院】

    第2章 对话框 11 第3章 硬件菜单 17 第4章 输入选择组件 19 第5章 图片和按钮 30 第6章 日期和时间控件 34 第7章 进度条和滑动条 37 第8章 GridView和Gallery控件 43 第9章 AutoCompleteTextView提示输入建议 48 第...

    Android实训实习报告总结.docx

    如对话框背景等,或去google code找些开源的代码下来研究,学习人家的设计思想、模块功能的划分、代码组织结构等知识 这个过程中,涉及到的知识比较多,如版本管理工具SVN、Git、Mercurial,如设计模式Android实训...

    android开发资料大全

    最无私的Android资料(书籍+代码)分享[总结] Android中文帮助教程(非常合适新手入门) android程序编写及调试新手入门 大家一起学Android(Windows篇) android入门与提高必看指南 Android入门逆引手册 Android...

    Android实现底部图片选择Dialog

    自己这里总结一下,有需要开发者可以按照如下步骤直接使用即可。 1.效果图如下 点击选择照相后,弹出如下选择对话框: 2. Dialog实现 布局 &lt;LinearLayout xmlns:android=...

    Android开发案例驱动教程 配套代码

    采用案例驱动模式展开讲解知识点,即介绍案例-&gt;案例涉及技术-&gt;展开知识点-&gt;总结的方式 本书作者从事多年一线开发和培训,讲解知识点力求细致,深入浅出 目 录 前言 第1章 Android操作系统概述 1 1.1 Android...

Global site tag (gtag.js) - Google Analytics