標籤:風飛雪未揚 從零開始學android toast土司提示組件
在系統之中,通過對話方塊可以對使用者的某些操作進行提示,但是在Android平台之中也提供了另外一套更加友好的提示介面效果,而且這種介面在提示使用者的時候不會打斷使用者的正常操作,這種對話方塊可以通過Toast組件實現。Toast是一個以簡單提示資訊為主要顯示操作的組件,在Android之中android.widget.Toast的繼承結構如下所示:java.lang.Object ? android.widget.Toast 常用的方法:
| No. |
方法及常量 |
類型 |
描述 |
| 1 |
public static final int LENGTH_LONG |
常量 |
顯示時間長 |
| 2 |
public static final int LENGTH_SHORT |
常量 |
顯示時間短 |
| 3 |
public Toast(Context context) |
普通 |
建立Toast對象 |
| 4 |
public static Toast makeText(Context context, int resId, int duration) |
普通 |
建立一個Toast對象,並指定顯示文本資源的ID,和資訊的顯示時間 |
| 5 |
public static Toast makeText(Context context, CharSequence text, int duration) |
普通 |
建立一個Toast對象,並指定顯示文本資源,和資訊的顯示時間 |
| 6 |
public void show() |
普通 |
顯示資訊 |
| 7 |
public void setDuration(int duration) |
普通 |
設定顯示的時間 |
| 8 |
public void setView(View view) |
普通 |
設定顯示的View組件 |
| 9 |
public void setText(int resId) |
普通 |
設定顯示的文字資源ID |
| 10 |
public void setText(CharSequence s) |
普通 |
直接設定要顯示的文字 |
| 11 |
public void setGravity(int gravity, int xOffset, int yOffset) |
普通 |
設定組件的對齊 |
| 12 |
public View getView() |
普通 |
取得內部包含的View組件 |
| 13 |
public int getXOffset() |
普通 |
返回組件的X座標位置 |
| 14 |
public int getYOffset() |
普通 |
返回組件的Y座標位置 |
| 15 |
public void cancel() |
普通 |
取消顯示 |
XMl檔案配置
<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#DAAA" android:orientation="horizontal" android:padding="10dp" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" android:src="@drawable/kill" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:text="自訂的提示" /></LinearLayout></span>
JAVA檔案配置
<span style="font-size:14px;">package com.example.toast;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.Toast;public class MainActivity extends Activity {private Button button1, button2;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button1 = (Button) this.findViewById(R.id.button1);button2 = (Button) this.findViewById(R.id.button2);button1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stub// 原始的提示Toast.makeText(MainActivity.this, "自訂的提示框", Toast.LENGTH_SHORT).show();}});// 為button設定單擊事件button2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubView view = LayoutInflater.from(MainActivity.this).inflate(R.layout.linearlayout, null);// 將Layout布局轉換為View對象Toast toast = new Toast(MainActivity.this);// 建立Toast提示toast.setGravity(Gravity.CENTER, 0, 0);// 設定Toast提示的位置toast.setView(view);// 將布局增加到Toast組件當中toast.show();// 顯示提示}});}}</span>
原始提示效果:
自訂toast顯示
Toast組件較為簡單主要是為了對使用者的操作進行友好的提示,在開發當中較常用到,希望大家務必掌握
下節預報:ListView資料列表顯示組件