Android界面设计之对话框——定制Toast、AlertDialog
| 一、概述 |
在界面设计中需要根据用户操作显示提示信息、出错信息等,就要用到对话框。Android实现提示信息显示常用有两种方式
1、Toast
| 二、Toast |
Android中用来显示显示信息的一种机制,属于轻量级消息开发中使用频率很高。其特点
1、 不接受用户操作,没有焦点
2、 显示的时间有限,过一定的时间就会自动消失。
使用Toast显示信息非常简单,操作如下:
Toast toast=Toast.makeText(this, “数据加载完成”, Toast.LENGTH_LONG);//创建Toast toast.show();//显示消息
普通的Toast外观很简单,我们也可以根据需要定制Toast,如新闻头条中实现收藏和取消收藏的信息提示,如图所示效果


定制Toast主要有两个方面
1、 定制Toast的外观
2、 设置Toast在屏幕位置
实现定制Toast关键代码如下:
Toast toast = Toast.makeText(context, msg, Toast.LENGTH_LONG); //创建Toast
int offsetX = 0;
int offsetY = 0;
toast.setGravity(Gravity.BOTTOM, offsetX, offsetY);//设置位置
LinearLayout ll=LayoutInflater.from(context).inflate(R.Layout.custom_toast,null);
toast.setView(ll);//设置外观
toast.show();
上述案例实现完成代码如下:
1、CustomToastActivity:
private Button btCollect;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.custom_toast_activity);
initView();
}
private void initView(){
btCollect=(Button)super.findViewById(R.id.btCollect);
btCollect.setOnClickListener(this);
} private void showToast(String txt){
Toast tast=Toast.makeText(this, txt, Toast.LENGTH_LONG);
tast.setGravity(Gravity.CENTER, 0, 0);
View view=LayoutInflater.from(this).inflate(R.layout.custom_toast, null);
TextView tvMsg=(TextView)view.findViewById(R.id.tvMsg);
tvMsg.setText(txt);
tast.setView(view);
tast.show();
}
public void onClick(View view) {
String txtCollect=btCollect.getText().toString();
if(txtCollect.equals("收藏")){
btCollect.setText("取消收藏");
showToast("收藏成功");
}else{
btCollect.setText("收藏");
showToast("取消收藏");
}
}
2 、custom_toast.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="match_parent" android:background="@drawable/toast_shape" >
<TextView
android:id="@+id/tvImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="★"
android:textColor="@color/white"
android:layout_centerHorizontal="true"
>
</TextView> <TextView
android:id="@+id/tvMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28sp"
android:text="收藏成功"
android:textColor="@color/white"
android:layout_below="@+id/tvImg"
android:layout_centerHorizontal="true"
/> </RelativeLayout>
3、 toast_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#000000"></solid>
<corners android:radius="10dp"/>
</shape>
| 三、对话框 |
1、AlertDialog
简单的一种对话框,主要的目的是为用户显示一条警告信息,通过AlertDialog.Builder产生AlertDialog,主要代码如下:
Dialog dialog = new AlertDialog.Builder(this)
.setTitle("对话框") // 设置标题
.setMessage("显示提示信息。") // 显示信息
.setIcon(R.drawable.pic_m) // 设置显示的Icon
setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}) //设置按钮
.create(); // 创建Dialog
dialog.show(); // 显示对话框
其他主要方法:
setView : 给对话框设置自定义样式setItems :设置对话框要显示的一个list,一般用于显示几个命令时
setSingleChoiceItems:用来设置对话框显示一系列的单选框
setMultiChoiceItems :用来设置对话框显示一系列的复选框
setNeutralButton :普通按钮
setPositiveButton :给对话框添加"Yes"按钮
setNegativeButton :对话框添加"No"按钮create : 创建对话框
show :显示对话框
2、定制AlertDialog
主要使用getWindow().setContentView()设置外观,如实现退出应用对话框

Activity——CustomAlertDialog代码:
private void showExitDialog(){
final AlertDialog exitDlg=new AlertDialog.Builder(this).create();
exitDlg.show();//在添加内容之前执行
Window win=exitDlg.getWindow();
// 设置窗口的内容页面,为exit_dlg_layout.xml文件中定义view内容
win.setContentView(R.layout.exit_dlg_layout);
TextView tvMsg=(TextView)win.findViewById(R.id.tvMsg);
tvMsg.setText("要退出应用吗?");
Button btOk=(Button)win.findViewById(R.id.btOk);
Button btCancel=(Button)win.findViewById(R.id.btCancel);
// 为确认按钮添加事件,执行退出应用操作
btCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
exitDlg.dismiss();// 关闭对话框
}
});
btOk.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
finish();
exitDlg.dismiss();// 关闭对话框
}
});
}
public void onBackPressed(){//按回退键调用
showExitDialog();
}
exit_dlg_layout.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" android:padding="10dp">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content" android:orientation="vertical"
android:background="@drawable/exit_dlg_shape"
android:padding="5dp">
<TextView
android:layout_width="match_parent"
android:layout_height="30dp"
android:gravity="center"
android:textSize="20sp"
android:text="温馨提示"
android:textColor="#000000" />
<LinearLayout android:layout_width="match_parent"
android:layout_height="100dp" android:orientation="vertical" android:background="#ffffff">
<TextView
android:id="@+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:padding="5dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:padding="5dp"
android:layout_height="40dp" android:orientation="horizontal">
<Button
android:id="@+id/btOk"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"
android:layout_weight="1"
android:background="@drawable/comm_shop_detail_top_green"
android:layout_marginRight="2dp" />
<Button
android:id="@+id/btCancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="取消"
android:layout_weight="1"
android:background="@drawable/comm_shop_detail_top_black"
/>
</LinearLayout>
</LinearLayout> </RelativeLayout>
出处:http://www.cnblogs.com/jerehedu/
本文版权归烟台杰瑞教育科技有限公司和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
Android界面设计之对话框——定制Toast、AlertDialog的更多相关文章
- android 界面设计基本知识Ⅲ
本章继续讲述在android界面设计中相关的知识点.介绍内容包括BroadcastReceiver(广播),Service(服务),Widget(小部件),WebView(网页加载控件). 1.Bro ...
- android界面设计之布局管理
谈到android界面设计,各种布局样式不得不提!传统的布局方式有6种,我们会一一介绍. 在android studio2.2版本之后出现了一款超棒的布局方式,真正意义上的所见即所得,后面我们也会讲到 ...
- Android界面设计适配不同屏幕的尺寸和密度解读
Android是运行在各种提供不同的屏幕尺寸和密度的设备.Android系统提供跨设备的统一开发环境和处理大部分的工作,以调整每个应用程序的用户界面,以在其上显示的画面. 同时,该系统提供了API,允 ...
- android 界面设计基本知识Ⅱ
上一章讲述了Android界面设计时,一些基本控件的使用,本章主要讲述自定义控件,Fragment和Headler线程机制. 1.自定义控件 (1)基本知识 dp.sp和dx px:像素点 ...
- 让你大开眼界的10款Android界面设计
根据调查显示, iOS与Android的市场份额差距正越来越大.Android设备正在成为手机应用市场的主力军.如何从设计层面创造一个优美的app界面来吸引用户已然成为广大App开发者们必做的功课之一 ...
- android 界面设计基本知识
一个好的APP不仅有美观,好看的界面,更需要良好的性能和稳定性.作为一名开发人员,需要理解界面设计原则并写出优秀的界面设计代码. 本章主要讲述基本控件的使用,界面布局及一些常用的界面设计属性. 1.常 ...
- 第三篇-以LinearLayout进行Android界面设计
一.新建一个项目 选择empty activity,此时项目里面没有main.java的文件. 二.手动创建java文件 Project那儿选择android模式,在app/java/com....一 ...
- 第四篇-以ConstraintLayout进行Android界面设计
此文章基于第三篇. 一.新建一个layout.xml文件,创建方法不再赘述,在Design界面右击LinearLayout,点击Convert LinearLayout to ConstraintLa ...
- android 界面设计
wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE); DisplayMetrics dm = new DisplayMetric ...
随机推荐
- Xshell拖拽上传文件插件
lrzsz是一款在linux里可代替ftp上传和下载的程序.在linux中支持直接拖拽上传的插件:同时也支持rz和sz进行命令上传和下载. 插件安装 yum -y install lrzsz 上传(r ...
- ZOJ2112 BZOJ1901 Dynamic Rankings 树套树 带修改的区间第k小
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2112 树套树,线段树套splay或者主席树套树状数组,我抄了一下hzwer ...
- Go语言特点
作者:asta谢链接:https://www.zhihu.com/question/21409296/answer/18184584来源:知乎 1.Go有什么优势 可直接编译成机器码,不依赖其他库,g ...
- java后台接收json数据,报错com.alibaba.fastjson.JSONObject cannot be cast to xxx
从前台接收json封装的list数据,在后台接收时一直报错,com.alibaba.fastjson.JSONObject cannot be cast to xxx, 使用这种方式接收可以接收 @R ...
- Codeforces Round #355 (Div. 2) D. Vanya and Treasure 分治暴力
D. Vanya and Treasure 题目连接: http://www.codeforces.com/contest/677/problem/D Description Vanya is in ...
- SDWebImage支持WebP格式图片
SDWebImage本身就已经支持了webp格式的图片 1.下载libwebp https://github.com/webmproject/libwebp 然后你需要先安装好有homebrew或者m ...
- 极路由通过SSH添加静态路由表之后无法跳转的问题
1.确定系统已经开启了转发功能: /etc/sysctl.conf下的配置项目为net.ipv4.ip_forward = 1 2.关闭防火墙的REJECT,也就是修改/etc/config/fire ...
- Towelroot v3.0版发布 将支持更多设备 Towelroot v3.0下载
Towelroot虽然已经发布一段时间了,虽然所Towelroot可以一键ROOT很多设备,虽然它只有100多K.不过还是有一小部分的机型没办法ROOT成功的,也不知道什么原因.不过不用担心,Geoh ...
- Netdata----Linux 性能实时监测工具
https://my-netdata.io/ https://github.com/firehol/netdata/wiki http://soluck.iteye.com/blog/2291618
- SQL Server 中添加表注释
今天在创建完表之后,发现没有办法给表添加注释说明,字段的注释可以在建表的时候就添加,上网查了一下使用SQL给表添加注释的方法,方法如下: -- 表加注释 EXEC sys.sp_addextended ...