Android对话框介绍--AlertDialog简单介绍
来源:互联网 发布:淘宝优惠券app排名 编辑:程序博客网 时间:2024/06/09 14:12
在我们的Android开发过程中,有多种对话框,比如:Dialog、AlertDialog、ProgressDialog、时间对话框等等。而在这里我们就主要简单的讲解一下AlertDialog
AlertDialog是Dialog的一个直接子类,一般而言,我们使用的对话框都会有一个标题(title),一个图像(Icon),提示内容(message),还有一个(button)、或者两个、或者三个按钮(一般而言三个按钮的比较少见)。
有一点使我们必须要注意的:AlertDialog的构造方法全部是Protected类型的,所以我们是无法直接new 一个AlertDialog,而是需要采用AlertDialog.Builder中的create()方法来创建对话框。
写一个简单布局,我们就设定一个按钮,当我们点击这个按钮的时候就触发出一个AlertDialog
<?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"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="AlertDialog" /></LinearLayout>
创建MainActivity.class:
public class MainActivity extends AppCompatActivity { private Button button; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); //设置标题 builder.setTitle("提示"); //设置提示内容 builder.setMessage("你确定要删除这条信息吗?"); //使用图片 builder.setIcon(R.mipmap.ic_launcher); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"你点击了取消",Toast.LENGTH_SHORT).show(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"你点击了确定",Toast.LENGTH_SHORT).show(); } }); builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"你点击了忽略",Toast.LENGTH_SHORT).show(); } }); AlertDialog alertDialog = builder.create(); alertDialog.show(); } }); }}
我们这里添加了三个按钮,其中Android中已经帮我把第一设置为“忽略”,当然,如果我们是使用了两个按钮的话,那么第一个按钮就是“取消”,这主要是为了我们在进行某项操作时会多一个思考,防止下意识的就确定,造成不可挽回的后果;setNeutralButton这个用的较少。诚然,对于对话框还有很多属性可以set,读者可以多多尝试。对于AlertDialog的简单介绍就到这了。
0 0
- Android对话框介绍--AlertDialog简单介绍
- Android之对话框AlertDialog及其子类的介绍(一)
- AlertDialog(对话框)的介绍和使用
- android--widget介绍之AlertDialog
- Android简单弹出对话框AlertDialog的使用
- android对话框介绍
- android对话框介绍
- 简单对话框AlertDialog
- AlertDialog 对话框简单Dome
- Android中的对话框AlertDialog
- 【Android】对话框 AlertDialog
- Android对话框之AlertDialog
- Android 对话框 (AlertDialog & ProgressDialog)
- android AlertDialog对话框
- 【Android】对话框 AlertDialog
- 【Android】对话框 AlertDialog
- Android 重写对话框AlertDialog
- 【Android】对话框 AlertDialog
- svn+maven 生成hotfix打包的版本号无法自动更新
- 线性回归
- FZU-2148 求n个点构成的凸四边形的个数
- 描述在浏览器中敲入一个网址并按下回车后所发生的事情
- 获取网页链接
- Android对话框介绍--AlertDialog简单介绍
- BGP 选路属性验证
- POJ1759_Garland_二分::应该枚举哪个值
- 4建造者模式(Builder)
- JS统计一个JSON元素数量
- opencv安装
- 一个笛卡尔积的优化
- 我们生活在地狱,但很幸运。
- 项目自动打包部署脚本