Android uses custom PopupWindow to implement custom Toast

Source: Internet
Author: User

The title is a bit sloppy. In fact, you can write two blog posts respectively. That is to say, I can understand two aspects after reading this article:

  • Custom Layout Toast

  • Custom PopupWindow implement multi-function Toast

First perceptual knowledge:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0646393921-0.gif "title =" Toast.gif "/>

The trigger event is from The onClick of MenuItem. For details, see the Android custom Menu

I. custom Layout Toast

Layout file 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>

Use this custom layout to solve repeated Toast Pop-up.

/*** Display custom 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 ();}

Ii. Custom PopupWindow to implement multi-function Toast

The following is the complete code of the MenuItem click event, which is commented out in detail.

@ Overridepublic void onClick (View v) {/** MenuItem dismiss */mPopWindow. dismiss ();/*** custom PopupWindow simulation function 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 (t Rue); // set the mPopToast to disappear when you touch the outside. setAnimationStyle (android. r. style. animation_Dialog); // sets the animation effect mPopToast. showAtLocation (v, Gravity. BOTTOM | Gravity. CENTER, 0,300);/*** use PopupWindow to simulate the Toast, where the TextView click event. * View events cannot be processed because Toast does not obtain focus. * PopupWindow solves this issue of focus. */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 ();/** display custom Toast again, this time triggered by TextView in PopupWindow */View toastViewAgain = mLayoutInflater. inflate (R. layout. dialog_toast, null); showToast (toastViewAgain) ;}});/** display custom Toast */View toastView = mLayoutInflater. inflate (R. layout. dialog_toast, null); showToast (toastView );}

The Toast implemented using PopupWindow is not limited by the time limit of Toast. LENGTH_SHORT or Toast. LENGTH_LONG, and can obtain the focus to interact with users, such as onClick.

Toast itself is a very convenient prompt component, which features convenient use, such

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

Such a code can give users a friendly prompt.

If you want to make complex interaction functions, these are not the positioning of Toast. You need to customize the PopupWindow or AlertDialog, and their positioning is here.


This article from the "wIsper technology into art" blog, please be sure to keep this source http://lichen.blog.51cto.com/697816/1216906

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.