編輯:關於Android編程
本文實例講述了Android Toast通知用法。分享給大家供大家參考,具體如下:
Toast在手機屏幕上向用戶顯示一條信息,一段時間後信息會自動消失。
1.默認用法
復制代碼 代碼如下:Toast.makeText(getApplicationContext(), "默認Toast樣式",Toast.LENGTH_SHORT).show();
2.Fragment中的用法
復制代碼 代碼如下:Toast.makeText(getActivity(),"網絡連接錯誤,請檢察網絡設置", Toast.LENGTH_LONG).show();
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>
更多關於Android相關內容感興趣的讀者可查看本站專題:《Android編程之activity操作技巧總結》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數據庫技巧總結》、《Android操作json格式數據技巧總結》、《Android數據庫操作技巧總結》、《Android編程開發之SD卡操作方法匯總》、《Android開發入門與進階教程》、《Android視圖View技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
iPhone用戶從來不用在意剩余內存的多少,也無需考慮太多的系統安全性問題,因為封閉的iOS從“根兒”上就比開放的Android靠譜
日期顯示和選擇類庫,可以用來選擇一段連續的和多個不連續的日期,具體的UI完全抽象出來了,可以高度自定義(GITHUB地址)支持的功能:1、選擇一段連續的和多個不連續的日期
package com.example.app; import java.util.ArrayList; import java.util.HashMap; imp
Android自定義動態布局 — 多圖片上傳 本文介紹Android中動態布局添加圖片,多圖片上傳。 項目中效果圖: 技術點