Android开发之自定义Toast(带详细注释)
因为工作需求,所以自己研究了自定义Toast,这里做出总结:
在此之前有一点需要提前说明:Toast与其他组件一样,都属于UI界面中的内容,因此在子线程中无法使用Toast弹出提示内容,如果强行在子线程中增加会导致错误。本例子使用异步任务来说明,原理跟子线程一样。
主界面:
自定义Toast弹出的样式:
MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btn_define; //自定义Toast
Button btn_thread; //线程中的Toast @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initButton();
btn_define.setOnClickListener(this);
btn_thread.setOnClickListener(this);
} private void initButton() {
btn_define = (Button) findViewById(R.id.btn_all_define);
btn_thread = (Button) findViewById(R.id.btn_other_thread);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_all_define:
ToastMessage("自定义Toast", "这是我自定义的Toast");
break;
case R.id.btn_other_thread:
//异步任务
new AsyncTask<String, Void, Object>() {
@Override
protected void onPreExecute() {
super.onPreExecute();
} protected Object doInBackground(String... strings) {
//线程中无法使用Toast,需要将Toast发送至主线程中才能使用
Message msg = new Message();
msg.what = 1;//标记位,标记是哪个线程传数据
msg.obj = "这是线程的toast";
mHandler.sendMessage(msg);
return null;
} @Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
}
}.execute(); break;
}
} Handler mHandler = new MyHandler(); private class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case 1:
String str = (String) msg.obj;
Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show();
break;
}
}
} /**
* 将Toast封装在一个方法中,在其它地方使用时直接输入要弹出的内容即可
*/
private void ToastMessage(String titles, String messages) {
//LayoutInflater的作用:对于一个没有被载入或者想要动态载入的界面,都需要LayoutInflater.inflate()来载入,LayoutInflater是用来找res/layout/下的xml布局文件,并且实例化
LayoutInflater inflater = getLayoutInflater();//调用Activity的getLayoutInflater()
View view = inflater.inflate(R.layout.toast_style, null); //加載layout下的布局
ImageView iv = view.findViewById(R.id.tvImageToast);
iv.setImageResource(R.mipmap.atm);//显示的图片
TextView title = view.findViewById(R.id.tvTitleToast);
title.setText(titles); //toast的标题
TextView text = view.findViewById(R.id.tvTextToast);
text.setText(messages); //toast内容
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 12, 20);//setGravity用来设置Toast显示的位置,相当于xml中的android:gravity或android:layout_gravity
toast.setDuration(Toast.LENGTH_LONG);//setDuration方法:设置持续时间,以毫秒为单位。该方法是设置补间动画时间长度的主要方法
toast.setView(view); //添加视图文件
toast.show();
}
}
activity的布局文件:activity_main.xml
<?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:gravity="center_horizontal"
android:orientation="vertical"> <Button
android:id="@+id/btn_all_define"
android:text="自定义Toast"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/> <Button
android:id="@+id/btn_other_thread"
android:text="线程中Toast的使用"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"/>
</LinearLayout>
toast的样式:toast_style.xml
我这里是比较简单的样式,如果想要其他形式的可以根据自身的需求更改界面即可实现。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffffff"
android:orientation="vertical" > <TextView
android:id="@+id/tvTitleToast"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="1dip"
android:background="#bb000000"
android:gravity="center"
android:textColor="#ffffffff" /> <LinearLayout
android:id="@+id/llToastContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="1dip"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:background="#44000000"
android:orientation="vertical"
android:padding="15dip" > <ImageView
android:id="@+id/tvImageToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" /> <TextView
android:id="@+id/tvTextToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:textColor="#ff000000" />
</LinearLayout> </LinearLayout>
以上就是今天上午研究的自定义的Toast。以后开发直接拿来用就OK!
补充:上面的形式总归上不了大雅之堂,现在补充一个新的完全自定义的Toast,可以根据自身需求随意设置:
import com.example.myproject.R;
import android.content.Context;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast; public class ToastUtils {
private Toast toast;
private LinearLayout toastView; /**
* 完全自定义布局Toast
*/
public ToastUtils(Context context, View view, int duration) {
toast = new Toast(context);
toast.setView(view);
toast.setDuration(duration);
} /**
* 向Toast中添加自定义View
*/
public ToastUtils addView(View view, int position) {
toastView = (LinearLayout) toast.getView();
toastView.addView(view, position);
return this;
} /**
* 设置Toast字体及背景
*/
public ToastUtils setToastBackground(int messageColor, int background) {
View view = toast.getView();
if (view != null) {
TextView message = (TextView) view.findViewById(R.id.message);
message.setBackgroundResource(background);
message.setTextColor(messageColor);
} return this;
} /**
* 短时间显示Toast
*/
public ToastUtils Short(Context context, CharSequence message) {
if (toast == null
|| (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_SHORT);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_SHORT);
}
return this;
} /**
* 长时间显示toast
*/
public ToastUtils Long(Context context, CharSequence message) {
if (toast == null
|| (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(Toast.LENGTH_LONG);
}
return this;
} /**
* 自定义显示Toast的时长
*/
public ToastUtils Indefinite(Context context, CharSequence message,
int duration) {
if (toast == null
|| (toastView != null && toastView.getChildCount() > 1)) {
toast = Toast.makeText(context, message, duration);
toastView = null;
} else {
toast.setText(message);
toast.setDuration(duration);
} return this;
} /**
* 显示Toast
*/
public ToastUtils show() {
toast.show();
return this;
} /**
* 获取Toast
*/
public Toast getToast() {
return toast;
}
}
Android开发之自定义Toast(带详细注释)的更多相关文章
- Android开发之自定义的ListView(UITableViewController)
Android开发中的ListView, 顾名方法思义,就是表视图.表示图在iOS开发中就是TableView.两者虽然名称不一样,但是其使用方法,使用场景以及该控件的功能都极为相似,都是用来展示大量 ...
- android开发之自定义组件
android开发之自定义组件 一:自定义组件: 我认为,自定义组件就是android给我们提供的的一个空白的可以编辑的图片,它帮助我们实现的我们想要的界面,也就是通过自定义组件我们可以把我们要登入的 ...
- C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式
C#/WPF/WinForm/.NET程序代码实现软件程序开机自动启动的两种常用方法的示例与源码下载带详细注释-源码代码-注册表方式-启动目录快捷方式 C#实现自动启动的方法-两种方法 源码下载地址: ...
- android开发中系统自带语音模块的使用
android开发中系统自带语音模块的使用需求:项目中需要添加语音搜索模块,增加用户体验解决过程:在网上搜到语音搜索例子,参考网上代码,加入到了自己的项目,完成产品要求.这个问题很好解决,网上能找到很 ...
- Light OJ - 1026 - Critical Links(图论-Tarjan算法求无向图的桥数) - 带详细注释
原题链接 无向连通图中,如果删除某边后,图变成不连通,则称该边为桥. 也可以先用Tajan()进行dfs算出所有点 的low和dfn值,并记录dfs过程中每个 点的父节点:然后再把所有点遍历一遍 ...
- Android开发之自定义组件和接口回调
说到自定义控件不得不提的就是接口回调,在Android开发中接口回调用的还是蛮多的.在这篇博客开始的时候呢,我想聊一下iOS的自定义控件.在iOS中自定义控件的思路是继承自UIView, 在UIVie ...
- Android开发之自定义局部导航菜单
如今,要实现导航功能方案有很多.比如: 1.用3.0+自带的Toolbar + Fragment导航. 2.用Tabhost实现导航.小弟学浅,就只用过这两种方案实现导航. 但是这两种方案都有一个很明 ...
- Android开发UI之Toast的使用
Toast,A toast provides simple feedback about an operation in a small popup. 对于操作提供一个简单反馈信息. 官网链接:htt ...
- Android开发进阶——自定义View的使用及其原理探索
在Android开发中,系统提供给我们的UI控件是有限的,当我们需要使用一些特殊的控件的时候,只靠系统提供的控件,可能无法达到我们想要的效果,这时,就需要我们自定义一些控件,来完成我们想要的效果了.下 ...
随机推荐
- Android Monkey: “No activities found to run, monkey aborted”错误原因
用monkey测试app时,输入命令adb shell monkey -p com.example.test -v -500 发现报错, 错误输入: :Monkey: seed=13 count=5 ...
- tensorflow降低版本
tensorflow降低版本: pip install tensorflow==1.2.0 查看版本: import tensorflow as tf print(tf.__version__)
- js高级-执行上下文
全局上下文 方法1() 压入 (栈的数据结构 先进后出)push() pop() 1.当一个函数在调用另外一个函数的时候新调用的函数会行成一个新的执行上下文 压入执行环境栈的栈顶 2.浏览器js执 ...
- php mysql 查询判断周几
$where .= " and (DAYOFWEEK( from_unixtime(`px_time`, '%Y-%m-%d')) = 1)"; //周日从1开始
- grabcut 分割 Rect
#include "opencv2/opencv.hpp" using namespace cv; void main() { Mat src = imread("E:\ ...
- Linq to sql 之 ExecuteQuery 错误:指定的转换无效
问题:通过dbContext.ExecuteQuery 得到数据并赋值给一个集合. 代码: public IEnumerable<LeaveCodeSum> GetLeavTotal(st ...
- 安装scrapy框架
前提安装好python.setuptools. 1.安装Python 安装完了记得配置环境,将python目录和python目录下的Scripts目录添加到系统环境变量的Path里.在cmd中输入py ...
- 无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确
安装好后,登录后台提示 无法连接mysql,请检查mysql是否已启动及用户密码是否设置正确 检查mysql是否启动netstat -lnpt是否有3306端口? 一 有A 检查/www/wdlinu ...
- CentOS rpm
rpm命令是RPM软件包的管理工具.rpm原本是Red Hat Linux发行版专门用来管理Linux各项套件的程序,由于它遵循GPL规则且功能强大方便,因而广受欢迎.逐渐受到其他发行版的采用.RPM ...
- js改变表单的内容样式
一.改变单个样式 var obj = document.getElementById("id"); obj.style.cssText = " display: ...