android 33 对话框控件
对话框控件:最多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 对话框控件的更多相关文章
- Android 中常见控件的介绍和使用
1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...
- 【转】Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用
Android M新控件之AppBarLayout,NavigationView,CoordinatorLayout,CollapsingToolbarLayout的使用 分类: Android UI ...
- 【风马一族_Android】第4章Android常用基本控件
第4章Android常用基本控件 控件是Android用户界面中的一个个组成元素,在介绍它们之前,读者必须了解所有控件的父类View(视图),它好比一个盛放控件的容器. 4.1View类概述 对于一个 ...
- Android 一个日历控件的实现代码
转载 2017-05-19 作者:Othershe 我要评论 本篇文章主要介绍了Android 一个日历控件的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看 ...
- Android笔记---常用控件以及用法
这篇文章主要记录下Android的常用控件以及使用的方法,Android 给我们提供了大量的UI控件,合理地使用这些控件就可以非常轻松地编写出相当不错的界面,这些是Android学习的基础,没有什么业 ...
- CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建
Android开发使用控件入门--环境搭建 2014-12-24 09:57 14人阅读 评论(0) 收藏 编辑 删除 CAD控件.CAD三维控件,手机 ...
- Android开发使用控件入门--环境搭建
Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...
- winform对话框控件、打印控件
对话框控件: ColorDialog:颜色选择对话框,让用户自行选择一种颜色,使用方法类似FontDialog FontDialog:字体选择对话框,让用户自行选择一种字体(也可以选择字体颜色,需要在 ...
- Android中ListView控件的使用
Android中ListView控件的使用 ListView展示数据的原理 在Android中,其实ListView就相当于web中的jsp,Adapter是适配器,它就相当于web中的Servlet ...
随机推荐
- new作为修饰符
new 修饰符与 new 操作符是两个概念 new 修饰符用于声明类或类的成员,表示隐藏了基类中同名的成员.而new 操作符用于实例化一个类型 new 修饰符只能用于继承类,一般用于弥补基类设计的不足 ...
- Google开源库-Volley
Android平台的网络通信库,使是网通信 更快,更简单,更健壮 适合场景: 数据量不大,通信 频繁. 大数据,流媒体是不适合使用的 * 它主要是帮我们载入和缓存从远程网络加载的图片 * 所有的 ...
- User Commands
archive Creates a hadoop archive[v.存档; n.档案文件; 档案室; ]. More information can be found at Hadoop Archi ...
- C#小数点位数处理方法
//方法一: //保留小数位数,并能四舍五入 DecimalFormat de = new DecimalFormat("0.00"); System.out.println(de ...
- CocoaPods ADD private Spec Repo
Private Pods CocoaPods is a great tool not only for adding open source code to your project, but als ...
- button 垂直分布
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];//button的类型 button.frame = CGRectMak ...
- 手动删除文件夹exe病毒并恢复原来文件夹
转自手动删除文件夹exe病毒并恢复原来文件夹 经常使用U盘.MP3.MP4等移动硬盘的大家,有时是不是会发现,移动硬盘里有现了exe文件,原来本来有一个文件夹的名字是 音乐 ,但后来发现 音乐 这个文 ...
- java 表格项的删除、编辑、增加 修改版
修改之后的java 代码: package com.platformda.optimize; import java.awt.BorderLayout; import java.awt.Button; ...
- 14.8.4 Moving or Copying InnoDB Tables to Another Machine 移动或者拷贝 InnoDB 表到另外机器
14.8.4 Moving or Copying InnoDB Tables to Another Machine 移动或者拷贝 InnoDB 表到另外机器 这个章节描述技术关于移动或者复制一些或者所 ...
- [LeetCode#276] Paint Fence
Problem: There is a fence with n posts, each post can be painted with one of the k colors. You have ...