AlterDialog对话框的使用
2024-09-05 03:11:31
第一步先写出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();
}
}
第三步完成
最新文章
- jQuery设置聚焦并使光标位置在文字最后
- POJ 3415 后缀数组
- APP落地页开发中的一些小经验~
- 时事新闻之 谷歌 google 发布Tensor Flow 源代码
- 实现在Android开发中的Splash Screen开场屏的效果
- css3之圆角效果 border-radius
- 在 Java EE 组件中使用 Camel Routes
- linux中less命令使用
- 双向队列 STL
- 文件锁及其实例,底层文件I/O操作,基本文件操作和实例,Linux中文件及文件描述符概述
- 连不上VSS 【转】
- php语法标记风格
- hadoop集群篇--从0到1搭建hadoop集群
- 高仿QQ头像截取
- 20175208《Java程序设计》第五周学习总结
- ASP.NET Core 启动流程图
- [UE4]死亡后调整视角
- iOS 图标
- 详细讲解WaterRefreshLoadMoreView的使用
- 深入理解ajax系列第四篇
热门文章
- linux vim基本操作
- Sql Server 数字金额转中文金额 函数
- 利用socket编程在ESP32上搭建一个TCP客户端
- unity, 替换shader渲染(Rendering with Replaced Shaders)【转】
- 不用第三方软件–一键开关笔记本电脑wifi热点的批处理
- vue和小程序的相似之处
- Centos7安装gitlab11 学习笔记之基础概念、部署安装、权限管理、issue管理
- (4.36)sql server中的waitfor
- springboot用controller跳转html页面
- 第6章:LeetCode--数组(冒泡排序、快速排序)