Toast的用法(可以設置顯示時間,自定義布局的,線程中的Toast)


      

自定義的Toast類

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/button01_id"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示自定義toast" />
    
    <Button
        android:id="@+id/button02_id"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示可以控制顯示時間的toast" />
    
    <Button
        android:id="@+id/button03_id"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="顯示由代碼創建的toast" />
    
    <Button
        android:id="@+id/button04_id"
        android:layout_marginTop="20dp"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="在其他線程中的toast" />
</LinearLayout>

 

自定義toast的布局界面

 

toast.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_id"
    android:orientation="vertical"
    android:padding="10dp"
    android:layout_width="200dp"
    android:layout_height="150dp"
    android:background="#000000">
    
    <ImageView 
        android:id="@+id/image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center_horizontal"
        android:src="@drawable/kale"/>
    
    <TextView 
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="toast"
        android:textColor="#FFF"/>
    
</LinearLayout>

 

myToast.java

package com.kale.toast;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.Toast;

/*
 * Toast自定義顯示時間
 * 使用方法
 * 1.先初始化類 MyToast myToast = new MyToast(this);
 * 2.顯示消息   myToast.setText("要顯示的內容");//設置要顯示的內容
 *             myToast.show(8000);  //傳入消息顯示時間,單位毫秒,最少2000毫秒,並且只能是2000的倍數。
 *             傳入0時會一直顯示,只有調用myToast.cancel();時才會取消。
 * 3.取消消息顯示   myToast.cancel();
 * */

public class MyToast {

    private Context mContext = null;
    private Toast mToast = null;
    private Handler mHandler = null;
    private int duration = 0;
    private int currDuration = 0;
    private final int DEFAULT=2000;
    private Runnable mToastThread = new Runnable() {
    
    public void run() {
        mToast.show();
        mHandler.postDelayed(mToastThread, DEFAULT);// 每隔2秒顯示一次
        if (duration != 0) {
            if (currDuration <= duration) {
                currDuration += DEFAULT;
            } 
            else {
                cancel();
            }
        }
        
        }
    };
    
    public MyToast(Context context) {
        mContext = context;
        currDuration=DEFAULT;
        mHandler = new Handler(mContext.getMainLooper());
        mToast = Toast.makeText(mContext, "", Toast.LENGTH_LONG);
    }
    
    public void setText(String text) {
        mToast.setText(text);
    }
    
    public void show(int duration) {
        this.duration = duration;
        mHandler.post(mToastThread);
    }
    
    public void setGravity(int gravity, int xOffset, int yOffset){
        mToast.setGravity(gravity, xOffset, yOffset);
    }
    
    public void setDuration(int duration){
        mToast.setDuration(duration);
    }
    
    public void setView(View view){
        mToast.setView(view);
    }
    
    public void cancel( ) {
        mHandler.removeCallbacks(mToastThread);// 先把顯示線程刪除
        mToast.cancel();// 把最后一個線程的顯示效果cancel掉,就一了百了了
        currDuration = DEFAULT;
    }
}

 

MainActivity.java

package com.kale.toast;

import android.app.Activity;
import android.os.Bundle;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

    Button btn01,btn02,btn03,btn04;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        viewInit();
        ButtonListener listener = new ButtonListener();
        btn01.setOnClickListener(listener);
        btn02.setOnClickListener(listener);
        btn03.setOnClickListener(listener);
        btn04.setOnClickListener(listener);
        
        
    }
    
    public class ButtonListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            // TODO 自動生成的方法存根
            switch (v.getId()) {
            case R.id.button01_id:
                firstToast();
                break;
            case R.id.button02_id:
                secondToast();
                break;
            case R.id.button03_id:
                thirdToast();
                break;
            case R.id.button04_id:
                otherToast();
            default:
                break;
            }
        }
        
    }


    public void firstToast() { //獲取LayoutInflater對象,該對象能把XML文件轉換為與之一直的View對象  
        LayoutInflater inflater = getLayoutInflater();  
        //根據指定的布局文件創建一個具有層級關系的View對象  
        //第二個參數為View對象的根節點,即LinearLayout的ID  
        View layout = inflater.inflate(R.layout.toast, 
                (ViewGroup) findViewById(R.id.toast_layout_id));  
          
        //查找ImageView控件  
        //注意是在layout中查找  
        ImageView image = (ImageView) layout.findViewById(R.id.image);  
        image.setImageResource(R.drawable.kale);  
        TextView text = (TextView) layout.findViewById(R.id.text);  
        text.setText("自定義Toast");  
        
          Toast toast = new Toast(getApplicationContext());
        //設置Toast的顯示位置,后兩個參數是偏移量
        toast.setGravity(Gravity.CENTER, 0, 100);
        toast.setView(layout);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }
    
    public void secondToast() {
        View view = getLayoutInflater().inflate(R.layout.toast, null);
        MyToast myToast = new MyToast(MainActivity.this);
        myToast.setText("顯示時間為8000毫秒");//設置要顯示的內容
        myToast.setView(view);
        myToast.show(8000);  //傳入消息顯示時間,單位毫秒,最少2000毫秒,並且只能是2000的倍數。
    }
    
    public void thirdToast() {
        ImageView image = new ImageView(getApplicationContext());
        image.setImageResource(R.drawable.ic_launcher);
        
        LinearLayout ll = new LinearLayout(getApplicationContext());
        ll.addView(image);
        
        Toast toast = new Toast(getApplicationContext());
        toast.setView(ll);
        toast.setDuration(0);
        toast.show();
    }
    
    public void otherToast() {
        System.out.println("other");
        new Thread(){
            @Override
            public void run() {
                 Looper.prepare();//先移除
                 Toast.makeText(getApplicationContext(),"在其他線程中的toast",Toast.LENGTH_SHORT).show();  
                 Looper.loop();// 進入loop中的循環,查看消息隊列
            }
         }.start();

    }

    
    private void viewInit() {
        btn01 = (Button)findViewById(R.id.button01_id);
        btn02 = (Button)findViewById(R.id.button02_id);
        btn03 = (Button)findViewById(R.id.button03_id);
        btn04 = (Button)findViewById(R.id.button04_id);
    }
}

 

源碼下載:http://download.csdn.net/detail/shark0017/7654053


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM