Android Toast通知用法實例詳解
本文實例講述了Android Toast通知用法。分享給大家供大家參考,具體如下:
Toast在手機屏幕上向用戶顯示一條信息,一段時間后信息會自動消失。
1.默認用法
2.Fragment中的用法
3.自定義顯示位置效果
toast = Toast.makeText(getApplicationContext(), "自定義位置Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();
4.帶圖片效果
toast = Toast.makeText(getApplicationContext(), "帶圖片的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.icon); toastView.addView(imageCodeProject, 0); toast.show();
5.完全自定義效果
LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast)); ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast); image.setImageResource(R.drawable.icon); TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); title.setText("Attention"); TextView text = (TextView) layout.findViewById(R.id.tvTextToast); text.setText("完全自定義Toast"); toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();
6.其他線程
Main.java代碼:
package com.wjq.toast; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class Main extends Activity implements OnClickListener { Handler handler = new Handler(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.btnSimpleToast).setOnClickListener(this); findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this); findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this); findViewById(R.id.btnCustomToast).setOnClickListener(this); findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this); } public void showToast() { handler.post(new Runnable() { @Override public void run() { Toast.makeText(getApplicationContext(), "我來自其他線程!",Toast.LENGTH_SHORT).show(); } }); } @Override public void onClick(View v) { Toast toast = null; switch (v.getId()) { case R.id.btnSimpleToast: Toast.makeText(getApplicationContext(), "默認Toast樣式",Toast.LENGTH_SHORT).show(); break; case R.id.btnSimpleToastWithCustomPosition: toast = Toast.makeText(getApplicationContext(), "自定義位置Toast",Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); break; case R.id.btnSimpleToastWithImage: toast = Toast.makeText(getApplicationContext(), "帶圖片的Toast",Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); LinearLayout toastView = (LinearLayout) toast.getView(); ImageView imageCodeProject = new ImageView(getApplicationContext()); imageCodeProject.setImageResource(R.drawable.icon); toastView.addView(imageCodeProject, 0); toast.show(); break; case R.id.btnCustomToast: LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast)); ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast); image.setImageResource(R.drawable.icon); TextView title = (TextView) layout.findViewById(R.id.tvTitleToast); title.setText("Attention"); TextView text = (TextView) layout.findViewById(R.id.tvTextToast); text.setText("完全自定義Toast"); toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show(); break; case R.id.btnRunToastFromOtherThread: new Thread(new Runnable() { public void run() { showToast(); } }).start(); break; } } }
main.xml代碼:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" android:orientation="vertical" android:padding="5dip" > <Button android:id="@+id/btnSimpleToast" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="默認" > </Button> <Button android:id="@+id/btnSimpleToastWithCustomPosition" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="自定義顯示位置" > </Button> <Button android:id="@+id/btnSimpleToastWithImage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="帶圖片" > </Button> <Button android:id="@+id/btnCustomToast" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="完全自定義" > </Button> <Button android:id="@+id/btnRunToastFromOtherThread" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="其他線程" > </Button> </LinearLayout>
custom.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/llToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#ffffffff" android:orientation="vertical" > <TextView android:id="@+id/tvTitleToast" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="1dip" android:background="#bb000000" android:gravity="center" android:textColor="#ffffffff" /> <LinearLayout android:id="@+id/llToastContent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="1dip" android:layout_marginLeft="1dip" android:layout_marginRight="1dip" android:background="#44000000" android:orientation="vertical" android:padding="15dip" > <ImageView android:id="@+id/tvImageToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" /> <TextView android:id="@+id/tvTextToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:paddingLeft="10dip" android:paddingRight="10dip" android:textColor="#ff000000" /> </LinearLayout> </LinearLayout>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android下Activity全屏顯示實現(xiàn)方法
這篇文章主要介紹了Android下Activity全屏顯示實現(xiàn)方法,以兩種不同的方法來實現(xiàn)這一技巧,非常具有實用性,需要的朋友可以參考下2014-10-10android實現(xiàn)圖片閃爍動畫效果的兩種實現(xiàn)方式(實用性高)
本文通過兩種方法給大家講解了android實現(xiàn)圖片閃爍動畫效果,實用性非常高,對這兩種方法感興趣的朋友一起通過本文學習吧2016-09-09Android_UI 仿QQ側(cè)滑菜單效果的實現(xiàn)
相信大家對QQ側(cè)滑菜單的效果已經(jīng)不陌生了吧,側(cè)滑進入個人頭像一側(cè),進行對頭像的更改,我的收藏,QQ錢包,我的文件等一系列的操作,下面小編給大家分享Android_UI 仿QQ側(cè)滑菜單效果的實現(xiàn),一起看看吧2017-04-04Android中利用App實現(xiàn)消息推送機制的代碼
Android中利用App實現(xiàn)消息推送機制的代碼,需要的朋友可以參考下。2011-05-05Android中TextView動態(tài)設(shè)置縮進距離的方法
項目需求如果在項目中第一行文字需要添加布局的情況我們應該怎么做呢,經(jīng)過一番考慮和查找我最終選擇了縮進的方式解決這個問題,這篇文章主要給大家介紹了關(guān)于Android中TextView動態(tài)設(shè)置縮進距離的相關(guān)資料,需要的朋友可以參考下2022-04-04Android程序開發(fā)之UIScrollerView里有兩個tableView
這篇文章主要介紹了UIScrollerView里有兩個tableView 的相關(guān)資料,需要的朋友可以參考下2016-04-04