AndroidでのToast使用について

4604 ワード

AndroidのToastは、ユーザーにヘルプ/ヒントを表示するために使用されます.
以下はToastのいくつかの使用についての総括であり、皆さんの学習と問題解決に役立つことを望んでいます.
既定のToastスタイル:
Toast.makeText(getApplicationContext(), "  Toast  ",Toast.LENGTH_SHORT).show();

カスタム位置Toast:
Toast toast = Toast.makeText(getApplicationContext(), "     Toast",Toast.LENGTH_LONG);  
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

カスタムサイズToast(カスタムサイズはコードではなくレイアウトで制御されます.レイアウトのpaddingプロパティを使用することに注意してください)
View params=LayoutInflater.from(getApplicationContext()).inflate(R.layout.params_layout, null);
TextView paramsTextView=(TextView) params.findViewById(R.id.paramsTitleToast);
paramsTextView.setText("      Toast");
toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(params);

画像付きToast
Toast toast  = Toast.makeText(getApplicationContext(), "    Toast",Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout) toast.getView();
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.ic_launcher);
toastView.addView(imageCodeProject, 0);
toast.show();

完全カスタムToast
View layout = getLayoutInflater().inflate(R.layout.custom, null);
ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.ic_launcher);
TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);
title.setText("Attention");
TextView text = (TextView) layout.findViewById(R.id.tvTextToast);
text.setText("     Toast");
Toast toast = new Toast(getApplicationContext()); 
toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

Toast繰り返し表示の問題
問題の説明:1つのToastがまだ状態を表示している時、別のToastのshow()方法を呼び出した時、2つのToastは1つの列の中に入ることができて、前のToastの表示が終わった後に、後の1つはやっと表示することができて、誤操作などの原因の存在のため、Toastの長い時間の存在するユーザーの体験の問題を招きます.
解決策:Toastを作成するたびに判断し、前にToastが表示されている場合は、ToastのsetText()メソッドを呼び出して表示する情報を置き換えるだけです.
public class CustomToast { 
    private static Toast mToast;
    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            mToast.cancel();
        }
    };

    public static void showToast(Context mContext, String text, int duration)      
        mHandler.removeCallbacks(r);
        if (mToast != null)
            mToast.setText(text);
        else
            mToast = Toast.makeText(mContext, text, Toast.LENGTH_SHORT);
        mHandler.postDelayed(r, duration);
        mToast.show();
    }

    public static void showToast(Context mContext, int resId, int duration) {
        showToast(mContext, mContext.getResources().getString(resId), duration);
    }
}

最後に1つのプロジェクトを提供します.
Toastの統合管理クラスToastUtil:
/**
 * Toast     
 * 
 */
public class ToastUtil {

	private ToastUtil() {
		/* cannot be instantiated */
		throw new UnsupportedOperationException("cannot be instantiated");
	}

	public static boolean isShow = true;

	/**
	 *      Toast
	 * 
	 * @param context
	 * @param message
	 */
	public static void showShort(Context context, CharSequence message) {
		if (isShow)
			Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	/**
	 *      Toast
	 * 
	 * @param context
	 * @param message
	 */
	public static void showShort(Context context, int message) {
		if (isShow)
			Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
	}

	/**
	 *      Toast
	 * 
	 * @param context
	 * @param message
	 */
	public static void showLong(Context context, CharSequence message) {
		if (isShow)
			Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}

	/**
	 *      Toast
	 * 
	 * @param context
	 * @param message
	 */
	public static void showLong(Context context, int message) {
		if (isShow)
			Toast.makeText(context, message, Toast.LENGTH_LONG).show();
	}

	/**
	 *      Toast  
	 * 
	 * @param context
	 * @param message
	 * @param duration
	 */
	public static void show(Context context, CharSequence message, int duration) {
		if (isShow)
			Toast.makeText(context, message, duration).show();
	}

	/**
	 *      Toast  
	 * 
	 * @param context
	 * @param message
	 * @param duration
	 */
	public static void show(Context context, int message, int duration) {
		if (isShow)
			Toast.makeText(context, message, duration).show();
	}

}