本文主要记录的是如何自定义Toast样式,以及如何防止连续点击几次重复出现Toast提示的问题。
针对第一个问题:自定义 Toast样式,它的基本流程大致如下:
1.设计一个自己需要的布局(既可通过xml布局文件,也可在程序中动态创建需要的控件)。
2.程序中加载布局(加载自定义的xml布局)
3.把布局设置给Toast对象,显示出来
第二个小问题:防止重复显示Toast提示。系统默认的Toast是:当一次显示还未结束,又一次请求显示时,Toast提示会在上一次显示结束时再一次显示,这显然不是很有必要。对此,可以通过记录两次Toast显示请求的时间间隔,来判断发出请求时,上一次显示是否结束。如果显示已结束,则响应请求,反之,不响应。
接着,通过代码来展示一下:
1.自定义Toast的布局文件toast.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:background="#9BCD9B"
android:orientation="horizontal" >
<ImageView
android:paddingLeft="5dp"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/t3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="40dp"
android:id="@+id/toast_text"
android:textColor="#0000ff"
android:gravity="center_vertical"
android:text="测试"
android:paddingRight="10dp"
/>
</LinearLayout>
2.主布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:layout_width="match_parent"
android:gravity="center"
android:textSize="18sp"
android:textStyle="bold"
android:layout_height="wrap_content"
android:text="@string/toast_" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="不重复显示"
android:onClick="oneTest"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="默认显示"
android:onClick="twoTest"/>
</LinearLayout>
3.一个简单的工具类
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
public class MyToast {
static int lastTime=0;
public MyToast(Context context) {
}
public static void showToast(Context context, String message,int position,int time){
if(((int)System.currentTimeMillis()-lastTime)>time){//时间间隔判断
View view =LayoutInflater.from(context).inflate(R.layout.toast, null);//加载自定义布局
Toast toast=new Toast(context);
TextView textTV=(TextView) view.findViewById(R.id.toast_text);
toast.setDuration(time);
textTV.setText(message);
toast.setGravity(position, 0, 0);
toast.setView(view);//把布局设置到Toast对象中
toast.show();
lastTime=(int) System.currentTimeMillis();
}
}
}
4.测试程序
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
private MyToast mToast;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void oneTest(View view){
MyToast.showToast(MainActivity.this,"不重复显示", Gravity.CENTER, 2000);
}
public void twoTest(View view){
Toast.makeText(getApplicationContext(), "默认显示", Toast.LENGTH_SHORT).show();
}
}
5.实现效果:
源码下载地址http://download.youkuaiyun.com/detail/u012221316/9409612