第一步先写出layout文件

<?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"> <TextView
android:id="@+id/tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="单选对话框"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:textColor="#fffdb371"/> <Button
android:id="@+id/bt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="设置字体大小"
android:layout_marginTop="20dp"
android:layout_gravity="center"/> </LinearLayout>

第二步写MainActity

package com.iang.dialongdemo;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView textView;
private int[] textSizeArr={10,20,25,30,40};
int textSize=1;
// 加载资源文件,找到资源文件中的按钮id号
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.linear);
findViewById(R.id.bt).setOnClickListener(this);
textView=(TextView) findViewById(R.id.tv);
} @Override
public void onClick(View v) {
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(this)
.setTitle("设置字体大小")
.setIcon(R.mipmap.ic_launcher)
.setSingleChoiceItems(new String[]{"小号", "默认", "中号", "大号", "超大"}, textSize, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textSize=which;
}
})
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
textView.setTextSize(textSizeArr[textSize]);
dialog.dismiss();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
// 回退程序的使用
@Override
public void onBackPressed() {
// super.onBackPressed();
AlertDialog dialog;
AlertDialog.Builder builder =new AlertDialog.Builder(this)
.setTitle("普通对话框") //设置对话框的标题
.setIcon(R.mipmap.ic_launcher) //设置标题图标
.setMessage("是否确定退出应用:")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
MainActivity.this.finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog = builder.create();
dialog.show();
}
}

第三步完成

AlterDialog对话框的使用的更多相关文章

  1. 对话框 AlterDialog

    AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("尊敬的用户"); bu ...

  2. Android中的AlertDialog使用示例一(警告对话框)

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,AlertDialog实现方法为建造者模式. ...

  3. android继承Dialog实现自定义对话框

    有时需要自定义对话框,可以使用AlterDialog.Bulider,比如下面的代码片段 new AlertDialog.Builder(self) .setTitle("标题") ...

  4. android常用的四种对话框java代码

    AlterDialog:确认取消警告提示框 public void showAlertDialog(View view){ AlertDialog.Builder dialog = new Alert ...

  5. 一步步开发自己的博客 .NET版(10、前端对话框和消息框的实现)

    关于前端对话框.消息框的优秀插件多不胜数.造轮子是为了更好的使用轮子,并不是说自己造的轮子肯定好.所以,这个博客系统基本上都是自己实现的,包括日志记录.响应式布局.评论功能等等一些本可以使用插件的.好 ...

  6. jQuery遮罩层登录对话框

    用户登录是许多网站必备的功能.有一种方式就是不管在网站的哪个页面,点击登录按钮就会弹出一个遮罩层,显示用户登录的对话框.这用方式比较灵活方便.而现在扫描二维码登录的方式也是很常见,例如QQ.微信.百度 ...

  7. Android系统默认对话框添加图片

    开发工具Android Studio 今天公司UI要求软件对话框改成加图片的,以前没有做过,所以就学习了一下,废话不多说, 看效果: 创建XML文件dialog_lsit_item.xml <L ...

  8. Android—关于自定义对话框的工具类

    开发中有很多地方会用到自定义对话框,为了避免不必要的城府代码,在此总结出一个工具类. 弹出对话框的地方很多,但是都大同小异,不同无非就是提示内容或者图片不同,下面这个类是将提示内容和图片放到了自定义函 ...

  9. 使用CSS3的box-shadow实现双透明遮罩层对话框

    box-shadow介绍 在我之前的一篇文章<从天猫和支付宝身上学习opcity与rgba>中,介绍了实现双透明遮罩层效果的两种方法,分别是opacity和rgba.他们需要分别依赖于不同 ...

随机推荐

  1. Kafka Connect Architecture

    Kafka Connect's goal of copying data between systems has been tackled by a variety of frameworks, ma ...

  2. PAT 甲级 1037 Magic Coupon (25 分) (较简单,贪心)

    1037 Magic Coupon (25 分)   The magic shop in Mars is offering some magic coupons. Each coupon has an ...

  3. (三)UML之类图关系

    泛化(generalization):表示is-a的关系,是对象之间耦合度最大的一种关系,子类继承父类的所有细节.直接使用语言中的继承表达.在类图中使用带三角箭头的实线表示,箭头从子类指向父类.如下图 ...

  4. redis列表和有序集合

    redis中的list数据类型是可以插入重复数据的,有去重的需求的话可以用redis有序集合数据类型 Redis Zadd 命令用于将一个或多个成员元素及其分数值加入到有序集当中. 如果某个成员已经是 ...

  5. Java使用JDBC连接数据库逐条插入数据、批量插入数据、以及通过SQL语句批量导入数据的效率对比

    测试用的示例java代码: package com.zifeiy.test.normal; import java.io.File; import java.io.FileOutputStream; ...

  6. iOS-KVO(转)

    参考学习网址:http://blog.sina.com.cn/s/blog_71715bf8010166ut.html KVO就是NSKeyValueObserving的缩写,它也是Foundatio ...

  7. jvm minor gc 为什么比 full gc 快很多

    1.minor gc 也需要STW,只不过正常情况下 minor gc  STW时间非常短,所以很多人误以为没有STW. 这里的正常情况是,Eden 区产生的新对象大部分被回收了,不需要拷贝. 2.M ...

  8. Laravel用户认证系统的实现细节

    原文地址:http://www.php.cn/php-weizijiaocheng-406076.html 这篇文章主要介绍了关于Laravel用户认证系统的实现细节,有着一定的参考价值,现在分享给大 ...

  9. CF1210A Anadi and Domino

    思路: 很有意思的思维题. 实现: #include <bits/stdc++.h> using namespace std; int check(vector<int>&am ...

  10. 【C/C++开发】C++实现简单的线程类

    C++封装一个简单的线程类 多线程编程简介: 大家在编程时,经常需要在程序中启动一个或多个线程来处理任务,而如果每次都是去调用系统创建线程的API函数来创建,代码量虽不多,但线程的创建和业务逻辑代码就 ...