標籤:
一、什麼是Toast
1、Toast是一種提供給使用者簡潔提示資訊的視圖。
2、該視圖以浮於應用程式之上的形式呈現給使用者, Toast提示介面不擷取焦點,在不影響使用者使用的情況下,給使用者某些提示。
3、Android提供的Toast類可以建立和顯示Toast資訊。
下面示範4種Toast的用法,普通的 Toast,已變更位元置的Toast,顯示圖片的Toast,自訂的Toast分別使用4個按鈕來實現。
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/btnShow" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Toast顯示"/> <Button android:id="@+id/btnShow2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Toast顯示位置"/> <Button android:id="@+id/btnShow3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Toast顯示圖片"/> <Button android:id="@+id/btnShow4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="自訂Toast顯示layout檔案"/></LinearLayout>
main.java
package com.example.zhengcheng.myapp;import android.app.Activity;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.Toast;public class MainActivity extends Activity { Button btnshow1; Button btnshow2; Button btnshow3; Button btnshow4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnshow1 = (Button) findViewById(R.id.btnShow); btnshow1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast toast = Toast.makeText(MainActivity.this, "測試Toast彈出訊息框", Toast.LENGTH_SHORT); toast.show(); } }); btnshow2 = (Button) findViewById(R.id.btnShow2); btnshow2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast toast = Toast.makeText(MainActivity.this, "更改Toast彈出位置", Toast.LENGTH_SHORT); //第一個參數設定Toast的相對位置,第二個參數為x座標(正為右,負為左),第三個參數為y座標(正為下,負為上) toast.setGravity(Gravity.CENTER, 0, -250); toast.show(); } }); btnshow3 = (Button) findViewById(R.id.btnShow3); btnshow3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast toast = Toast.makeText(MainActivity.this, " 帶圖片的Toast彈出對話方塊", Toast.LENGTH_SHORT); //擷取toast的布局方式 LinearLayout toast_layout = (LinearLayout) toast.getView(); ImageView iv = new ImageView(MainActivity.this); //建立圖片控制項 iv.setImageResource(R.mipmap.ic_launcher); //設定顯示圖片// toast_layout.addView(iv); //添加圖片控制項到toast布局中,預設添加到末尾 toast_layout.addView(iv, 0); //添加圖片控制項到toast布局中,添加到文字之前 toast.show(); //顯示圖片 } }); btnshow4 = (Button) findViewById(R.id.btnShow4); btnshow4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //擷取inflater對象,用來載入視圖 LayoutInflater inflater = LayoutInflater.from(MainActivity.this); View toast_view = inflater.inflate(R.layout.toast_layout,null); Toast toast = new Toast(MainActivity.this); //建立一個Toast toast.setView(toast_view); //設定Toast的視圖 toast.show(); //顯示 } }); }}
自訂Toast視圖 toast_layout.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="30dp" android:gravity="center" android:text="自訂的Toast對話方塊" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@android:drawable/sym_def_app_icon" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="顯示Toast彈出的內容" /></LinearLayout>
Android學習(十八)Toast的使用