歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android 用自定義PopupWindow實現自定義Toast

Android 用自定義PopupWindow實現自定義Toast

日期:2017/3/1 9:56:18   编辑:Linux編程

標題有點拗口,其實是可以分別寫成兩篇博文的。也就是說看完這篇能了解兩個方面:

自定義布局樣式的Toast

自定義PopupWindow實現多功能Toast

先感性認識:

觸發事件來自於MenuItem的onClick,具體請看上一篇Android 自定義Menu http://www.linuxidc.com/Linux/2013-06/85644.htm

--------------------------------------------------------------------------------

一 、自定義布局樣式的Toast


布局文件dialog_toast.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" >

<TextView

android:id="@+id/dialog_title"

android:textSize="@dimen/dialog_toast_title_text_size"

android:layout_width="@dimen/dialog_toast_width"

android:layout_height="wrap_content"

android:gravity="center_horizontal"

android:text="@string/toast_dialog_title"/>

<TextView

android:id="@+id/dialog_content"

android:layout_width="@dimen/dialog_toast_width"

android:layout_height="wrap_content"

android:drawableLeft="@drawable/frog"

android:drawableRight="@drawable/fungus"

android:layout_below="@id/dialog_title"

android:text="@string/toast_dialog_content"/>

</RelativeLayout>


使用這個自定義布局,解決多次Toast重復彈出。

/**

* 顯示自定義Toast<p>

* private Toast mToast;<br/>

* View toastView = mLayoutInflater.inflate(R.layout.dialog_toast, null);

* @param toastView

*/

private void showToast(View toastView) {

toastView.setBackgroundColor(Color.TRANSPARENT);

if (mToast == null) {

mToast = new Toast(mContext);

mToast.setView(toastView);

mToast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 100);

mToast.setDuration(Toast.LENGTH_SHORT);

}

mToast.show();

}

--------------------------------------------------------------------------------

二 、自定義PopupWindow實現多功能Toast


以下是MenuItem點擊事件的完整代碼,詳細注釋了。

@Override

public void onClick(View v) {

/** MenuItem dismiss */

mPopWindow.dismiss();

/**

* 自定義PopupWindow模擬多功能Toast

*/

View popToastView = mLayoutInflater.inflate(

R.layout.dialog_toast, null);

mPopToast = new PopupWindow(popToastView,

LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT, true);

mPopToast.setBackgroundDrawable(new ColorDrawable(

Color.TRANSPARENT));

mPopWindow.setOutsideTouchable(true);// 設置觸摸外面時消失

mPopToast

.setAnimationStyle(android.R.style.Animation_Dialog);// 設置動畫效果

mPopToast.showAtLocation(v,

Gravity.BOTTOM | Gravity.CENTER, 0, 300);

/**

* 用PopupWindow模擬Toast,裡面的TextView的點擊事件。

* 因為Toast不獲取焦點,所以View的事件無法處理。

* 而PopupWindow解決了這個焦點問題。

*/

TextView dialogContent = (TextView) popToastView

.findViewById(R.id.dialog_content);

dialogContent.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

if (mPopToast != null && mPopToast.isShowing()) {

mPopToast.dismiss();

/** 再次顯示自定義Toast, 這次是從PopupWindow裡面的TextView觸發的 */

View toastViewAgain = mLayoutInflater.inflate(

R.layout.dialog_toast, null);

showToast(toastViewAgain);

}

}

});

/** 顯示自定義Toast */

View toastView = mLayoutInflater.inflate(

R.layout.dialog_toast, null);

showToast(toastView);

}


使用PopupWindow實現的Toast可以不受Toast.LENGTH_SHORT或Toast.LENGTH_LONG的時間限制,並且可以得到焦點和用戶交互,比如onClick等等。

--------------------------------------------------------------------------------

Toast本身是一個非常便捷的提示組件,它的特點就是使用便捷,比如

Toast.makeText(mContext, "123", Toast.LENGTH_SHORT).show();


這樣的一句代碼就可以給出用戶一個很友好的提示。

如果想做出復雜的交互功能,這些就不是Toast的定位了,需要自定義PopupWindow或者AlertDialog來實現,它們的定位在此。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved