關於Toast我們常用的是:
Toast.makeText(context,"this is a Toast",Toast.LENGTH_SHORT).show();
一、Toast的封裝
但是這樣做有一個不好的地方,就是當我們點擊按鈕觸發Toast時,如果我們點擊了3次,Toast就會觸發3次。
如果用戶手抖了一下多點了幾次,導致Toast長時間關閉不了。又或者我們其實已經進行其他操作了,應該彈出
新的Toast提示了,而上個Toast還沒有結束,這樣的用戶體驗是不好的。
因此最好是將Toast做一次封裝,如下:
public class ToastUtils {
private static Toast sToast;
public static void showSimpleToast(Context context,String content){
if(sToast==null){
sToast=Toast.makeText(context,content,Toast.LENGTH_SHORT);
}else{
sToast.setText(content);
}
sToast.show();
}
調用的時候也很簡單,只需要把Context對象和Toast要顯示的內容傳進來就可以了:
ToastUtils.showSimpleToast(MainActivity.this,"普通的Toast信息");
sToast.setGravity(Gravity.BOTTOM,0,0);
imageView.setImageResource(R.mipmap.ic_launcher);
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.addView(imageView);
TextView textView=new TextView(MainActivity.this);
textView.setText("帶圖片的Toast信息");
ll.addView(textView);
sToast.setView(ll);
sToast.setDuration(Toast.LENGTH_SHORT);
package com.yzw.android.mytoast; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button mSimpleToast; private Button mImageToast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mSimpleToast=(Button) findViewById(R.id.simple_toast); mImageToast=(Button) findViewById(R.id.image_toast); mSimpleToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ToastUtils.showSimpleToast(MainActivity.this,"普通的Toast信息"); } }); mImageToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageView imageView=new ImageView(MainActivity.this); imageView.setImageResource(R.mipmap.ic_launcher); LinearLayout ll=new LinearLayout(MainActivity.this); ll.addView(imageView); TextView textView=new TextView(MainActivity.this); textView.setText("帶圖片的Toast信息"); ll.addView(textView); ToastUtils.showImageToast(MainActivity.this,ll); } }); } }
package com.yzw.android.mytoast; import android.content.Context; import android.view.Gravity; import android.widget.LinearLayout; import android.widget.Toast; /** * Created by yzw on 2016/8/24. */ public class ToastUtils { private static Toast sSimpleToast; private static Toast sImageToast; public static void showSimpleToast(Context context,String content){ if(sSimpleToast==null){ sSimpleToast=Toast.makeText(context,content,Toast.LENGTH_SHORT); }else{ sSimpleToast.setText(content); } sSimpleToast.show(); } public static void showImageToast(Context context,LinearLayout ll){ if(sImageToast==null){ sImageToast=new Toast(context); sImageToast.setGravity(Gravity.BOTTOM,0,0); sImageToast.setView(ll); sImageToast.setDuration(Toast.LENGTH_SHORT); }else { sImageToast.setView(ll); } sImageToast.show(); } }
<?xml version="1.0" encoding="utf-8"?> <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="com.yzw.android.mytoast.MainActivity"> <Button android:id="@+id/simple_toast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAllCaps="false" android:layout_gravity="center_horizontal" android:text="@string/simple_toast" /> <Button android:id="@+id/image_toast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAllCaps="false" android:layout_gravity="center_horizontal" android:text="@string/image_toast" /> </LinearLayout>