android自訂Toast之-彈出訊息

來源:互聯網
上載者:User

標籤:

android自訂Toast之-彈出訊息

實現方法:

1.new 一個Toast執行個體toast。

2.自訂一個顯示的View執行個體view 。

3.把toast.setView(view),toast.setDuration(Toast.LENGTH_LONG)設定顯訊息示時間

4.避免操作有誤一直重複彈出訊息處理,定義一個Toast的全域變數避免重複執行個體化進行控制

 下面是代碼

package com.android.hexiang.otptoken;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.content.Context;import android.widget.TextView;import android.widget.Toast;import com.android.hexiang.otptoken.R;/** * Created by hexiang on 2014/12/10. */public class MyToast {    private static Toast mToast = null;    /**     * 自訂訊息框     * @param c     * Created by hexiang on 2014/12/10.     */    @SuppressWarnings("deprecation")    private static Toast showCustomerToast(final Context c,String msg,int duration)    {        //擷取LayoutInflater對象,該對象可以將布局檔案轉換成與之一致的view對象        LayoutInflater inflater=LayoutInflater.from(c);        //將布局檔案轉換成相應的View對象        View layout=inflater.inflate(R.layout.my_customer_toast_layout,null);        layout.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.back_toast_style));        //從layout中按照id尋找TextView對象        TextView textView=(TextView)layout.findViewById(R.id.txt_msg);        //設定TextView的text內容        textView.setText(msg);        //執行個體化一個Toast對象        Toast toast=new Toast(c.getApplicationContext());        toast.setDuration(duration);        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 160);        toast.setView(layout);        return toast;    }    /**     * 長訊息框 指的的是時間長短     * Created by hexiang on 2014/12/10.     * @param c 上下文     * @param msg 訊息內容     */    @SuppressWarnings("deprecation")    public static void showCustomerToastLong(final Context c,String msg)    {   //避免重複彈出訊息        if (mToast == null) {            mToast =showCustomerToast(c,msg,Toast.LENGTH_LONG);        }        else {            TextView txt_msg=(TextView) mToast.getView().findViewById(R.id.txt_msg);            txt_msg.setText(msg);            mToast.setDuration(Toast.LENGTH_LONG);        }        mToast.show();    }    /**     * 短訊息框 指的的是時間長短 mToast.setDuration(Toast.LENGTH_SHORT);     * @param c 上下文     * @param msg 訊息內容     * Created by hexiang on 2014/12/10.     */    @SuppressWarnings("deprecation")    public static void showCustomerToastShot(final Context c,String msg)    {   //避免重複彈出訊息        if (mToast == null) {            mToast =showCustomerToast(c,msg,Toast.LENGTH_SHORT);        }        else {            TextView txt_msg=(TextView) mToast.getView().findViewById(R.id.txt_msg);            txt_msg.setText(msg);            mToast.setDuration(Toast.LENGTH_SHORT);        }        mToast.show();    }}

 

下面是XMl檔案

 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:id="@+id/toast_layout_root" 6     android:orientation="vertical" > 7  8   <TextView 9      android:layout_margin="6dp" 10     android:layout_gravity="center_vertical|center_horizontal"11     android:id="@+id/txt_msg"12     android:layout_width="match_parent"13     android:textColor="@color/white_light"14     android:textSize="14sp"15     android:text="this is a test toast"16     android:layout_height="wrap_content"17   />18 </LinearLayout>

下面是自訂漸層背景

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" >      <gradient        android:angle="315"        android:centerX="0.5"        android:centerY="0.5"        android:centerColor="#fcdede"        android:endColor="#e5edbe"        android:startColor="#d2b2b2" />    <corners android:radius="3dp" /></shape>

溫習下android:shape的使用
gradient:漸層
android:startColor和android:endColor分別為起始和結束顏色,android:centerColor中間部分顏色
ndroid:angle是漸層角度,必須為45的整數倍。

 

 

最後是調用

 1 MyToast.showCustomerToastLong(this,"測試訊息框"); 

 

大功告成了,本文謝絕轉載,自重 。程式猿不容易...

Created by hexiang on 2014/12/10. 清分依然

android自訂Toast之-彈出訊息

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.