您的位置:首页 > 其它

一个简单的Toast工具类---ToastUtil

2015-03-11 10:03 387 查看
好了,如果你对前几个小例子里面的Toast完全掌握的话,那么我们现在来创建一个Toast的工具类,以便于开发的简便。

编写一个借口,定义好各种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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: