使用AlertDialog类创建对话框
2016-07-23 10:53
316 查看
MainActivity.java
activity_main.xml
![](http://img.blog.csdn.net/20160723105542481)
“覆盖/忽略/取消”三重选项选择对话框
package com.example.ho.alertdialog_test; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_delete) .setTitle("是否删除文件?").setPositiveButton("确认", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int whichButton) { //点击”单击“按钮后,显示一个无按钮的对话框 new AlertDialog.Builder(MainActivity.this).setMessage("文件已经被删除") .create().show(); } } ).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int whichButton) { //单击取消按钮后,显示一个无按钮的对话框 new AlertDialog.Builder(MainActivity.this).setMessage("选择取消") .create().show(); } } ).show(); } }
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <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" tools:context="com.example.ho.alertdialog_test.MainActivity"> <Button android:id="@+id/button" android:text="显示确认/取消对话框" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
“覆盖/忽略/取消”三重选项选择对话框
@Override public void onClick(View v) { new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_notification_overlay) .setTitle("是否覆盖?").setPositiveButton("覆盖", new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog,int whichButton) { new AlertDialog.Builder(MainActivity.this).setMessage("文件已经覆盖") .create().show(); } } ).setNeutralButton("忽略", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int whichButton) { new AlertDialog.Builder(MainActivity.this).setMessage("忽略了") .create().show(); } } ).setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog,int whichButton) { new AlertDialog.Builder(MainActivity.this).setMessage("选择取消") .create().show(); } } ).show(); }
相关文章推荐
- hdu 2049 不容易系列之(4)——考新郎
- 将QQl里面的休息都迁移过来了
- C#实现HTML转图片(网页快照)
- 关于远程连接MySQL数据库的问题解决
- 程序员面试50题—指针的用法(5)
- 如何创建一个 Lua 模块
- Windows 7下配置JDK环境变量和Java环境变量配置
- 更少的直接百度,更多的取看API
- poj 3279 Pots
- 海量数据的处理(转)
- 图解手动全面检查管理本机端口
- 【VS开发】使用MFC创建并调用ActiveX控件
- 工作总结
- HDU 5738 Eureka
- Java进行二元操作类型转换
- Codeforces Round #364 (Div. 2)(A,B,C,D) 题解
- javascript运行机制之执行顺序详解
- Java多线程
- Unix编程(一)进程环境
- JAVA ACM 基础