对话框控件:最多3个按钮。

mainActivity.java

package com.sxt.day05_09;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setListener();
} private void setListener() {
setStandardDialogClickListener();
setItemsDialogClickListener();
setCustomDialogClickListener();
} //自定义对话框
private void setCustomDialogClickListener() {
findViewById(R.id.btnCustomDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//将custom_dialog.xml解析为一个View类型的java对象
View layout = View.inflate(MainActivity.this, R.layout.custom_dialog, null);
final EditText etAddrss=(EditText) layout.findViewById(R.id.etAddress);
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);//Builder是内部类
builder.setTitle("输入地址的对话框")//setTitle()返回builder对象(builder对象的地址),所以可以继续用点调用其他方法,
.setView(layout)//添加自定义布局,替换掉setMessage()
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String address=etAddrss.getText().toString();//内部类调用外部的方法的变量要是final类型
Log.i("main",address);
}
});
AlertDialog dialog = builder.create();//create最终创建对话框
dialog.show();//显示出来
}
});
} //列表类型的对话框
private void setItemsDialogClickListener() {
findViewById(R.id.btnItemsDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//真正创建对话框不是AlertDialog,而是这个类的内部类Builder,
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setItems(new String[]{"增加数据","删除数据","修改数据"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {//dialog是当前操作的AlertDialog对象,
switch (which) {
case 0:
Toast.makeText(MainActivity.this, "执行增加数据的操作", 3000).show();
break;
case 1:
Toast.makeText(MainActivity.this, "执行删除数据的操作", 3000).show();
break;
case 2:
Toast.makeText(MainActivity.this, "执行修改数据的操作", 3000).show();
break;
}
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
});
} //标准对话框
private void setStandardDialogClickListener() {
findViewById(R.id.btnStandardDialog).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//创建对话框的Builder对象,Builder是AlertDialog的内部类
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("退出考试窗口")
.setMessage("退出考试吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {//setPositiveButton创建肯定的按钮,按钮文本是"确定",点击按钮的监听器是DialogInterface.OnClickListener
//OnClickListener是DialogInterface的内部接口,
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("main","退出考试");
}
}).setNegativeButton("放弃", new DialogInterface.OnClickListener() {////setNegativeButton创建否定的按钮,按钮的文本和监听器
//如果不添加监听器,则仅仅关闭对话框没有动作。
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i("main","继续考试");
}
});
AlertDialog dialog = builder.create();//create最终创建对话框
dialog.show();//显示出来
}
});
} }

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btnStandardDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="标准对话框" />
<Button
android:id="@+id/btnItemsDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="列表对话框" />
<Button
android:id="@+id/btnCustomDialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义对话框" /> </LinearLayout>
custom_dialog.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" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="输入地址"/>
<EditText
android:id="@+id/etAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

android 33 对话框控件的更多相关文章

  1. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

  2. 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用

    Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...

  3. 【风马一族_Android】第4章Android常用基本控件

    第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...

  4. Android 一个日历控件的实现代码

    转载  2017-05-19   作者:Othershe   我要评论 本篇文章主要介绍了Android 一个日历控件的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看 ...

  5. Android笔记---常用控件以及用法

    这篇文章主要记录下Android的常用控件以及使用的方法,Android 给我们提供了大量的UI控件,合理地使用这些控件就可以非常轻松地编写出相当不错的界面,这些是Android学习的基础,没有什么业 ...

  6. CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建

    Android开发使用控件入门--环境搭建 2014-12-24 09:57     14人阅读     评论(0)     收藏         编辑     删除 CAD控件.CAD三维控件,手机 ...

  7. Android开发使用控件入门--环境搭建

    Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD  ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...

  8. winform对话框控件、打印控件

    对话框控件: ColorDialog:颜色选择对话框,让用户自行选择一种颜色,使用方法类似FontDialog FontDialog:字体选择对话框,让用户自行选择一种字体(也可以选择字体颜色,需要在 ...

  9. Android中ListView控件的使用

    Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...

随机推荐

  1. python路径函操作

    #判断是否为文件 os.path.isfile   #判断是否为目录 os.path.isdir   #返回文件名 os.path.basename(path)   #返回文件目录 os.path.d ...

  2. MyEclips:Struts 2 + Hibernate 4 + SQL Server2008

    步骤一:准备 1.下载 sqlJDBC.jar的下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=21599 Hibernate ...

  3. 扩展原生js的一些方法

    扩展原生js的Array类 Array.prototype.add = function(item){ this.push(item); } Array.prototype.addRange = fu ...

  4. 变更到Android4.4的问题

    更新到Android 4.4,写了个小程序.发现运行不起来了.抛空指针异常.debug模式下,发现在onCreate方法中获取Button是null. Android 4.4把layout进行了重组, ...

  5. Android模拟器常用命令收录

    一.Linux命令 1.挂载/systme分区为读写状态 mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system 2.切换为Root用户 ...

  6. ebook-nodej

    http://www.nodebeginner.org/index-zh-cn.html https://about.pinterest.com/careers/engineering/#oFwkXf ...

  7. Android AlertDialog更改标题颜色,字体等

    更改AlertDialog标题的方法google目前没有提供,只能通过其他办法 一种办法是:首先在源代码中找到有个叫AlertController的类,这个类就是AlertDialog的实现类,是没有 ...

  8. Git创建一个自己的本地仓库

    如果我们要把一个项目加入到Git的版本管理中,可以在项目所在的目录用git init命令建立一个空的本地仓库,然后再用git add命令把它们都加入到Git本地仓库的暂存区(stage or inde ...

  9. 优雅的让Fragment监听返回键

    转载请注明出处:http://write.blog.csdn.net/postedit/40507387 Activity可以很容易的得到物理返回键的监听事件,而Fragment却不能.假设Fragm ...

  10. 【HDOJ】3221 Brute-force Algorithm

    归来吧很好推导.T(n) = a^f(n-1)*b^f(n)%p.主要难点在于求mod和fibo.引用如下公式A^B%C = A^(B%phi(C) + phi(C))%C, 满足B>=phi( ...