Android Toast通知用法實(shí)例詳解
本文實(shí)例講述了Android Toast通知用法。分享給大家供大家參考,具體如下:
Toast在手機(jī)屏幕上向用戶顯示一條信息,一段時(shí)間后信息會(huì)自動(dòng)消失。
1.默認(rèn)用法
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(), "我來(lái)自其他線程!",Toast.LENGTH_SHORT).show(); } }); } @Override public void onClick(View v) { Toast toast = null; switch (v.getId()) { case R.id.btnSimpleToast: Toast.makeText(getApplicationContext(), "默認(rèn)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="默認(rèn)" > </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ù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android利用RecyclerView編寫(xiě)聊天界面
這篇文章主要為大家詳細(xì)介紹了Android利用RecyclerView編寫(xiě)聊天界面,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07Android下Activity全屏顯示實(shí)現(xiàn)方法
這篇文章主要介紹了Android下Activity全屏顯示實(shí)現(xiàn)方法,以兩種不同的方法來(lái)實(shí)現(xiàn)這一技巧,非常具有實(shí)用性,需要的朋友可以參考下2014-10-10android實(shí)現(xiàn)圖片閃爍動(dòng)畫(huà)效果的兩種實(shí)現(xiàn)方式(實(shí)用性高)
本文通過(guò)兩種方法給大家講解了android實(shí)現(xiàn)圖片閃爍動(dòng)畫(huà)效果,實(shí)用性非常高,對(duì)這兩種方法感興趣的朋友一起通過(guò)本文學(xué)習(xí)吧2016-09-09Android_UI 仿QQ側(cè)滑菜單效果的實(shí)現(xiàn)
相信大家對(duì)QQ側(cè)滑菜單的效果已經(jīng)不陌生了吧,側(cè)滑進(jìn)入個(gè)人頭像一側(cè),進(jìn)行對(duì)頭像的更改,我的收藏,QQ錢包,我的文件等一系列的操作,下面小編給大家分享Android_UI 仿QQ側(cè)滑菜單效果的實(shí)現(xiàn),一起看看吧2017-04-04Android實(shí)現(xiàn)文件存儲(chǔ)案例
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)文件存儲(chǔ)案例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼
Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼,需要的朋友可以參考下。2011-05-05Android編程之動(dòng)態(tài)壁紙實(shí)例分析
這篇文章主要介紹了Android編程之動(dòng)態(tài)壁紙實(shí)現(xiàn)方法,以實(shí)例形式分析了Android動(dòng)態(tài)壁紙的原理與實(shí)現(xiàn)步驟,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12Android中TextView動(dòng)態(tài)設(shè)置縮進(jìn)距離的方法
項(xiàng)目需求如果在項(xiàng)目中第一行文字需要添加布局的情況我們應(yīng)該怎么做呢,經(jīng)過(guò)一番考慮和查找我最終選擇了縮進(jìn)的方式解決這個(gè)問(wèn)題,這篇文章主要給大家介紹了關(guān)于Android中TextView動(dòng)態(tài)設(shè)置縮進(jìn)距離的相關(guān)資料,需要的朋友可以參考下2022-04-04Android 實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)
android使用Intent來(lái)實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn),接下來(lái)通過(guò)本文給大家分享Android 實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)的實(shí)例代碼,需要的的朋友參考下吧2017-09-09Android程序開(kāi)發(fā)之UIScrollerView里有兩個(gè)tableView
這篇文章主要介紹了UIScrollerView里有兩個(gè)tableView 的相關(guān)資料,需要的朋友可以參考下2016-04-04