Custom Toast with Image, Button, and TextView by using Android XML (特制機器人土司)

可以用MyToast.java , main.xml , mytoast.xml  來達到此目的


src/edu.android.mytoast/MyToast.java
 package edu.android.mytoast; 
 
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.Button;  
 import android.widget.Toast;  
 public class MyToast extends Activity {  
   /** Called when the activity is first created. */  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  

     Button tButton = (Button)findViewById(R.id.tButton);  
     tButton.setOnClickListener(new View.OnClickListener() {  
                @Override  
                public void onClick(View v) {  
                     LayoutInflater inflater = getLayoutInflater();  
                     View view = inflater.inflate(R.layout.mytoast,  
                         (ViewGroup) findViewById(R.id.toastLayout));  
                     Toast toast = new Toast(getApplicationContext());      
                     toast.setDuration(Toast.LENGTH_LONG);  
                     toast.setView(view);  
                     toast.show();  
                }  
           });  
   }  
 }  



沒有留言:

張貼留言