• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Android > android:常用的AlertDialog对话框及自定义对话框

android:常用的AlertDialog对话框及自定义对话框

作者:网友 字体:[增加 减小] 来源:互联网 时间:2017-05-26

网友通过本文主要向大家介绍了android alertdialog,android中alertdialog,alertdialog对话框,alertdialog 输入框,alertdialog等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

android:常用的AlertDialog对话框及自定义对话框


常用的Dialog有确认对话框,单选按钮对话框,多选按钮对话框,复选按钮对话框另外还有自定义的对话框

AlertDialog的常用方法

setTitle:为对话框设置标题

setMessage:为对话框设置内容

setIcon:为对话框设置图标

setItems设置对话框要显示的list

setMultiChoiceItems:一般用于复选框显示

setSingleChoiceItem:,设置单选按钮

setNeutralButton:普通按钮

setPositiveButton:添加确定按钮

setNegativeButton:添加取消按钮

setView:设置自定义样式

下面通过一个实例来了解这些方法

这是运行结果:

\

MainActivity.class

package com.example.alertdialog;

import android.R.bool;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.pm.LabeledIntent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {
	private Button button1;
	private Button button2;
	private Button button3;
	private Button button4;
	private Button button5;
	private int mark=0;
	private String item[] = { "学生", "工人", "教师", "农民" };
	private String multChoice[]={"旅游","电影","运动","读书"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button1 = (Button) findViewById(R.id.btn_button1);
		button2 = (Button) findViewById(R.id.btn_button2);
		button3 = (Button) findViewById(R.id.btn_button3);
		button4 = (Button) findViewById(R.id.btn_button4);
		button5 = (Button) findViewById(R.id.btn_button5);
		button1.setOnClickListener(this);
		button2.setOnClickListener(this);
		button3.setOnClickListener(this);
		button4.setOnClickListener(this);
		button5.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		//确认对话框
		case R.id.btn_button1: {
			AlertDialog.Builder builder = new Builder(this);
			builder.setIcon(R.drawable.ic_launcher);
			builder.setTitle("确认对话框");
			builder.setMessage("确认退出?");
			builder.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Toast.makeText(MainActivity.this, "你单击了确定按钮",
									Toast.LENGTH_SHORT).show();
						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							Toast.makeText(MainActivity.this, "你单击了取消按钮",
									Toast.LENGTH_SHORT).show();
						}
					});
			builder.create();
			builder.show();
			break;
		}
		//列表对话框
		case R.id.btn_button2: {
			AlertDialog.Builder builder = new Builder(this);
			builder.setIcon(R.drawable.ic_launcher);
			builder.setTitle("职业");
			builder.setItems(item, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					Toast.makeText(MainActivity.this, "你的职业是" + item[which],
							Toast.LENGTH_SHORT).show();
				}
			});
			builder.create();
			builder.show();
			break;
		}
		//多选对话框
		case R.id.btn_button3: {
			final boolean choose[]=new boolean[multChoice.length];
			AlertDialog.Builder builder = new Builder(this);
			builder.setIcon(R.drawable.ic_launcher);
			builder.setTitle("爱好");
			builder.setMultiChoiceItems(multChoice, null, new DialogInterface.OnMultiChoiceClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which, boolean isChecked) {
					// TODO Auto-generated method stub
					choose[which]=isChecked;
				}
			});
			builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					String result="";
					for(int i=0;i<multchoice.length;i++){ if(choose[i]){="" result+="multChoice[i]+"" ";="" }="" toast.maketext(mainactivity.this,="" "你的爱好["+result+"]",="" toast.length_short).show();="" });="" builder.create();="" builder.show();="" break;="" 单选对话框="" case="" r.id.btn_button4:="" {="" mark="0;" alertdialog.builder="" builder="new" builder(this);="" builder.seticon(r.drawable.ic_launcher);="" builder.settitle("职业");="" builder.setsinglechoiceitems(item,="" 0,="" new="" dialoginterface.onclicklistener()="" @override="" public="" void="" onclick(dialoginterface="" dialog,="" int="" which)="" todo="" auto-generated="" method="" stub="" builder.setpositivebutton("确定",="" "你的职业:"+item[mark],="" 自定义对话框="" r.id.btn_button5:="" layoutinflater="" inflater="LayoutInflater.from(this);" view="" null);="" builder.settitle("自定义对话框");="" builder.setview(view);="" builder.setneutralbutton("普通按钮",="" toast.maketext(mainactivity.this,"我是自定义的对话框哦",toast.length_short).show();="" 布局文件<="" pre=""><p>
</p><p>activity_main.xml</p><p>
</p><pre class="brush:java;"><relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <linearlayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"><button android:id="@+id/btn_button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="确认对话框"></button><button android:id="@+id/btn_button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="列表对话框"></button><button android:id="@+id/btn_button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="多选对话框"></button><button android:id="@+id/btn_button4" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="单选对话框"></button><button android:id="@+id/btn_button5" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定义对话框"></butt



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • AlertDialog的几种用法,alertdialog几种
  • Android新手入门2016(11)--非阻塞对话框AlertDialog
  • Android新手入门2016(12)--基于Layout文件的AlertDialog
  • Android阻止AlertDialog关闭,androidalertdialog
  • android:常用的AlertDialog对话框及自定义对话框

相关文章

  • 2017-05-26仿微信新消息提示音,消息
  • 2017-05-26Android 涂鸦最佳实践
  • 2017-05-26Android开发笔记(8)——调用子Activity,androidactivity
  • 2017-05-26Android中Fragment与Activity之间的交互(两种实现方式),androidfragment
  • 2017-05-26Android 框架启动流程
  • 2017-05-26可伸缩的textview,伸缩textview
  • 2017-05-26Android中SimpleAdapter的使用—自定义列表,自定义simpleadapter
  • 2017-05-26我的android学习经历16,android学习经历16
  • 2017-05-26Android 接入 OpenCV库的三种方式,androidopencv
  • 2017-05-26Android Studio第一次提交git使用,androidgit

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • android adb pull push,androidadb
    • 谈谈LoveLive SIF以及即将诞生的LL练习器,lovelivesif
    • [Android] Activity间切换,传递数据,androidactivity
    • android:异步任务asyncTask介绍及异步任务下载图片(带进度条)
    • RecyclerView的五大开源项目-解决办法
    • easy touch利用playmaker拖动ngui对象
    • Android中dip、dp、sp、pt和px的区别,androiddip
    • 硅谷新闻1--引导界面GuideActivity,1--guideactivity
    • Android DEX 基础,androiddex基础
    • 谷歌电子市场3--应用,谷歌电子市场3--

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有