Android Toast幾種使用方法:附源碼
Android Toast幾種使用方法:
一.預設展示:
Toast toast=Toast.makeText(getApplicationContext(), 預設的Toast, Toast.LENGTH_SHORT);
解釋:
(1).第一個參數:當前的上下文環境。可用getApplicationContext()或this
(2).第二個參數:要顯示的字串。也可是R.string中字串ID
(3).第三個參數:顯示的時間長短。Toast預設的有兩個LENGTH_LONG(長)和LENGTH_SHORT(短),也可以使用毫秒如2000ms
toast.show();
二.自訂顯示位置
Toast toast=Toast.makeText(getApplicationContext(), 自訂顯示位置的Toast, Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100); //螢幕置中顯示,X軸和Y軸位移量都是0 toast.setGravity(Gravity.CENTER, 0, 0); toast.show();
(1).第一個參數:設定toast在螢幕中顯示的位置。我現在的設定是置中靠頂
(2).第二個參數:相對於第一個參數設定toast位置的橫向X軸的位移量,正數向右位移,負數向左位移
(3).第三個參數:同的第二個參數道理一樣
如果你設定的位移量超過了螢幕的範圍,toast將在螢幕內靠近超出的那個邊界顯示
三、帶圖片的
Toast toast=Toast.makeText(getApplicationContext(), 顯示帶圖片的toast, 3000); toast.setGravity(Gravity.CENTER, 0, 0); //建立圖片視圖對象 ImageView imageView= new ImageView(getApplicationContext()); //設定圖片 imageView.setImageResource(R.drawable.ic_launcher); //獲得toast的布局 LinearLayout toastView = (LinearLayout) toast.getView(); //設定此布局為橫向的 toastView.setOrientation(LinearLayout.HORIZONTAL); //將ImageView在加入到此布局中的第一個位置 toastView.addView(imageView, 0); toast.show();
四、完全自訂顯示方式
ToastActivity.java類:
public class ToastActivity extends Activity { private Button bt; private ImageView image; private TextView title, content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showToast(); } }); } private void showToast() { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.toast, null); image = (ImageView) view.findViewById(R.id.image); title = (TextView) view.findViewById(R.id.title); content = (TextView) view.findViewById(R.id.content); image.setBackgroundResource(R.drawable.ic_launcher); title.setText(自訂toast); content.setText(hello,self toast); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_SHORT); toast.setView(view); toast.show(); }}
五、其他線程通過Handler的調用
//調用方法1 //Thread th=new Thread(this); //th.start(); //調用方法2 handler.post(new Runnable() { @Override public void run() { showToast(); } }); public void showToast(){ Toast toast=Toast.makeText(getApplicationContext(), Toast在其他線程中調用顯示, Toast.LENGTH_SHORT); toast.show(); } Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { int what=msg.what; switch (what) { case 1: showToast(); break; default: break; } super.handleMessage(msg); } }; @Override public void run() { handler.sendEmptyMessage(1); }
Util.java類:
package com.chengdong.su.toastutil;import android.content.Context;import android.support.v4.widget.DrawerLayout.LayoutParams;import android.view.Gravity;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Toast;public class Util { private static Toast mToast = null; public static int LENGTH_LONG = Toast.LENGTH_LONG; private static int LENGTH_SHORT = Toast.LENGTH_SHORT; /** * 普通簡訊提示 * * @param context * @param text * @param duration */ public static void TextToast(Context context, CharSequence text, int duration) { // 建立一個Toast提示訊息 mToast = Toast.makeText(context, text, duration); // 設定Toast提示訊息在螢幕上的位置 mToast.setGravity(Gravity.CENTER, 0, 0); // 顯示訊息 mToast.show(); } /** * 帶圖片訊息提示 * * @param context * @param ImageResourceId * @param text * @param duration */ public static void ImageToast(Context context, int imageResourceId, CharSequence text, int duration) { // 建立一個Toast提示訊息 mToast = Toast.makeText(context, text, Toast.LENGTH_LONG); // 設定Toast提示訊息在螢幕上的位置 mToast.setGravity(Gravity.CENTER, 0, 0); // 擷取Toast提示訊息裡原有的View View toastView = mToast.getView(); // 建立一個ImageView ImageView imageView = new ImageView(context); imageView.setImageResource(imageResourceId); // 建立一個LineLayout容器 LinearLayout linearLayout = new LinearLayout(context); linearLayout.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); linearLayout.setOrientation(LinearLayout.HORIZONTAL); // 向LinearLayout中添加ImageView和Toast原有的View linearLayout.addView(imageView); linearLayout.addView(toastView); // 將LineLayout容器設定為toast的View mToast.setView(linearLayout); // 顯示訊息 mToast.show(); }}