最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android Toast通知用法實(shí)例詳解

 更新時(shí)間:2016年07月13日 15:46:34   作者:dreamzml  
這篇文章主要介紹了Android Toast通知用法,集合實(shí)例形式詳細(xì)分析了Toast通知的基本使用方法與常見(jiàn)顯示布局效果實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了Android Toast通知用法。分享給大家供大家參考,具體如下:

Toast在手機(jī)屏幕上向用戶顯示一條信息,一段時(shí)間后信息會(huì)自動(dòng)消失。

1.默認(rèn)用法

復(fù)制代碼 代碼如下:
Toast.makeText(getApplicationContext(), "默認(rèn)Toast樣式",Toast.LENGTH_SHORT).show();

2.Fragment中的用法

復(fù)制代碼 代碼如下:
Toast.makeText(getActivity(),"網(wǎng)絡(luò)連接錯(cuò)誤,請(qǐng)檢察網(wǎng)絡(luò)設(shè)置", 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(), "我來(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 更新UI的方法匯總

    Android 更新UI的方法匯總

    這篇文章主要介紹了Android 更新UI的方法匯總的相關(guān)資料, 非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • Android圖像切換器imageSwitcher的實(shí)例應(yīng)用

    Android圖像切換器imageSwitcher的實(shí)例應(yīng)用

    這篇文章主要為大家詳細(xì)介紹了Android圖像切換器imageSwitcher的實(shí)例應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • Android四大組件之broadcast廣播使用講解

    Android四大組件之broadcast廣播使用講解

    Android開(kāi)發(fā)的四大組件分別是:活動(dòng)(activity),用于表現(xiàn)功能;服務(wù)(service),后臺(tái)運(yùn)行服務(wù),不提供界面呈現(xiàn);廣播接受者(Broadcast Receive),勇于接收廣播;內(nèi)容提供者(Content Provider),支持多個(gè)應(yīng)用中存儲(chǔ)和讀取數(shù)據(jù),相當(dāng)于數(shù)據(jù)庫(kù),本篇著重介紹廣播組件
    2022-12-12
  • Android仿IOS10圓盤時(shí)間選擇器

    Android仿IOS10圓盤時(shí)間選擇器

    這篇文章主要為大家詳細(xì)介紹了Android仿IOS10圓盤時(shí)間選擇器,自定義圓盤時(shí)間選擇器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • 詳解Android中的沉浸式狀態(tài)欄效果實(shí)例

    詳解Android中的沉浸式狀態(tài)欄效果實(shí)例

    本篇文章主要介紹了Android中的沉浸式狀態(tài)欄效果,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-12-12
  • Android編程實(shí)現(xiàn)圖片的上傳和下載功能示例

    Android編程實(shí)現(xiàn)圖片的上傳和下載功能示例

    這篇文章主要介紹了Android編程實(shí)現(xiàn)圖片的上傳和下載功能,涉及Android針對(duì)圖片的字節(jié)流轉(zhuǎn)換與傳輸操作相關(guān)技巧,需要的朋友可以參考下
    2016-10-10
  • Android?PickerScrollView滑動(dòng)選擇控件使用方法詳解

    Android?PickerScrollView滑動(dòng)選擇控件使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android?PickerScrollView滑動(dòng)選擇控件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • Android ArrayMap源代碼分析

    Android ArrayMap源代碼分析

    這篇文章主要介紹了Android ArrayMap源代碼分析的相關(guān)資料,需要的朋友可以參考下
    2016-10-10
  • Android Dialog對(duì)話框詳解

    Android Dialog對(duì)話框詳解

    這篇文章主要介紹了Android Dialog對(duì)話框詳解的相關(guān)資料,需要的朋友可以參考下
    2016-03-03
  • Android xml文件的序列化實(shí)現(xiàn)代碼

    Android xml文件的序列化實(shí)現(xiàn)代碼

    Android提供了XmlSerializer來(lái)實(shí)現(xiàn)XML文件的序列化。相比傳統(tǒng)方式,更高效安全,需要的朋友可以參考下
    2014-02-02

最新評(píng)論

富平县| 京山县| 金昌市| 莎车县| 丽江市| 桃园市| 龙山县| 麻江县| 灵台县| 平邑县| 兴义市| 平遥县| 浮梁县| 邓州市| 邵阳市| 霍邱县| 千阳县| 紫金县| 施秉县| 铅山县| 佛冈县| 高邮市| 太原市| 卓资县| 仁寿县| 桦南县| 仲巴县| 义马市| 金塔县| 桐城市| 普格县| 大兴区| 赤水市| 集贤县| 南康市| 美姑县| 青海省| 郯城县| 安陆市| 雅江县| 墨竹工卡县|