一个简单的Toast工具类---ToastUtil
2015-03-11 10:03
387 查看
好了,如果你对前几个小例子里面的Toast完全掌握的话,那么我们现在来创建一个Toast的工具类,以便于开发的简便。
编写一个借口,定义好各种Toast以后,用一个ToastUtil类来实现这个借口,并实现里面的方法,整体实现起来也不是很难,具体了解一下代码就知道怎么回事了,代码资源我会写在最后面。
具体代码如下:
布局文件:
这里的布局文件只是测试用,还有写了一个自定义Toast时候的布局
主要的布局文件:
自定义xml文件:
Java代码:
Toast的接口:
实现接口的类:
MainActivity也是测试类:
运行效果:
好了,如果你会了这个东西,差不多Toast的东西就可以告一段落了,Demo代码在下面
代码资源链接:
http://download.csdn.net/detail/u011539882/8490375
编写一个借口,定义好各种Toast以后,用一个ToastUtil类来实现这个借口,并实现里面的方法,整体实现起来也不是很难,具体了解一下代码就知道怎么回事了,代码资源我会写在最后面。
具体代码如下:
布局文件:
这里的布局文件只是测试用,还有写了一个自定义Toast时候的布局
主要的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.toastutil.MainActivity" android:orientation="vertical" tools:ignore="MergeRootFrame" > <Button android:id="@+id/btn_ToastDir" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/ToastDir" /> <Button android:id="@+id/btn_ToastWithPic" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/ToastWithPic" /> <Button android:id="@+id/btn_ToastPoi" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/ToastPoi" /> <Button android:id="@+id/btn_ToastBySelf" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/ToastBySelf" /> </LinearLayout>
自定义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" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="32dp" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/imageView1" android:layout_centerHorizontal="true" android:text="@string/hello_world" android:textAppearance="?android:attr/textAppearanceLarge" /> </RelativeLayout>
Java代码:
Toast的接口:
package com.example.toastutil; import android.content.Context; import android.view.View; /** * Toast的抽象类,主要用来规范有哪些方法,以便于维护 * */ public interface ToastFunctions { /** * 直接显示Toast * * @param context * 上下文 * @param text * 文本信息 * @param duration * 显示时长 */ public void immediateToast(Context context, String text, int duration); /** * 自定义位置的Toast * * @param context * 上下文 * @param text * 文本信息 * @param duration * 显示时长 * @param gravity * 位置 */ public void ToastLocation(Context context, String text, int duration, int gravity); /** * 带图片的Toast * * @param context * 上下文 * @param text * 文本信息 * @param duration * 显示时长 * @param resId * 图片资源id */ public void ToastWithPic(Context context, String text, int duration, int resId); /** * 自定义的Toast * * @param context * 上下文 * @param text * 文本信息 * @param duration * 显示时长 * @param view * 布局文件xml文件的view对象 <br><br> * * View的创建如下: <br> * //自定义Toast主要是利用LayoutInflater来实现<br> * LayoutInflater layoutInflater = getLayoutInflater(); <br> * //加载一个布局文件,自定义的布局文件 View<br> * view = layoutInflater.inflate(layoutId,null);<br> */ public void ToastBySelf(Context context, String text, int duration, View view); }
实现接口的类:
package com.example.toastutil; import android.content.Context; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; public class ToastUtil implements ToastFunctions { private Toast toast = null; @Override public void immediateToast(Context context, String text, int duration) { toast = Toast.makeText(context, text, duration); toast.show(); } @Override public void ToastLocation(Context context, String text, int duration, int gravity) { Toast toast = Toast.makeText(context, text, duration); toast.setGravity(gravity, 0, 0); toast.show(); } @Override public void ToastWithPic(Context context, String text, int duration, int resId) { toast = Toast.makeText(context, text, duration); // 通过Toast创建一个LinearLayout LinearLayout layout = (LinearLayout) toast.getView(); // 创建一个ImageView对象,并添加到LinearLayout中 ImageView imageView = new ImageView(context); imageView.setImageResource(resId); // 添加 layout.addView(imageView); // 显示Toast toast.show(); } @Override public void ToastBySelf(Context context, String text, int duration, View view) { // Toast toast = new Toast(context); // 设置Toast显示时长 toast.setDuration(duration); // 设置Toast的View对象 toast.setView(view); // 显示Toast toast.show(); } }
MainActivity也是测试类:
package com.example.toastutil; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.btn_ToastDir).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new ToastUtil().immediateToast(MainActivity.this, "最基础的Toast", Toast.LENGTH_SHORT); } }); findViewById(R.id.btn_ToastPoi).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new ToastUtil().ToastLocation(MainActivity.this,"自定义位置的Toast", Toast.LENGTH_SHORT, Gravity.CENTER); } }); findViewById(R.id.btn_ToastWithPic).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { new ToastUtil().ToastWithPic(MainActivity.this, "带图片的Toast", Toast.LENGTH_SHORT, R.drawable.ic_launcher); } }); findViewById(R.id.btn_ToastBySelf).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //自定义Toast主要是利用LayoutInflater来实现 LayoutInflater layoutInflater = getLayoutInflater(); //加载一个布局文件,自定义的布局文件 View<br> View view = layoutInflater.inflate(R.layout.layout_toast,null); new ToastUtil().ToastBySelf(MainActivity.this, "自定义Toast", Toast.LENGTH_SHORT,view); } }); } }
运行效果:
好了,如果你会了这个东西,差不多Toast的东西就可以告一段落了,Demo代码在下面
代码资源链接:
http://download.csdn.net/detail/u011539882/8490375
相关文章推荐
- ToastUtil,一个简单的Toast封装
- 写的一个简单的工具类,可以做对象类型的判断和迭代出一个对象所有属性的值
- 自定义一个简单的带图片的Toast
- 自定义ToastUtil工具类,可解决快速显示Toast文本问题
- 一个简单的Hibernate工具类HibernateUtil
- [UWP 开发] 一个简单的Toast实现
- 一个简单的String操作工具类
- 实现Android播放声音资源的一个简单的工具类
- 一个简单的Java模板工具类(二)—简单表达式解析实现
- 方法:一个简单的读取配置文件.properties的工具类 JAVA
- 一个简单的java对象序列化工具类
- 【一个简单封装的jdbc工具类】
- 一个简单封装的jdbc工具类
- 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类
- 实现一个简单的http请求工具类
- 实现一个配置简单功能强大的excel工具类搞定excel导入导出(二)
- 一个使用简单的工具类
- 一个简单封装的jdbc工具类
- Toast工具类及简单自定义Toast
- 简单的工具类LogUtil、Toast