使用Toast顯示提示信息框:不僅可以顯示簡單的提示信息,還可以顯示自定義的View
使用Toast顯示提示信息非常簡單:
1,調用Toast的構造器或者makeText()靜態方法創建一個Toast對象
2,調用Toast的方法來設置消息提示的對齊方式,頁邊距等
3,調用Toast的show()方法將它顯示出來
大部分時候Toast顯示的是簡單的消息文本;
~~~
Toast.makeText(MainActivity.this,?"簡單的提示信息",?Toast.LENGTH_SHORT).show()??
~~~
當要顯示自定義的View,包含圖片,列表之類的復雜提示時,需要調用Toast構造器創建實例,再調用setView方法設置該Toast的View組件
activity_main.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/simple"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示簡單提示" />
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="顯示帶圖片的提示"
/>
</LinearLayout>
~~~
MainActivity.java
~~~
package com.example.toast;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button simple = (Button) findViewById(R.id.simple);
// 為按鈕的單擊事件綁定事件監聽器
simple.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
// 創建一個Toast提示信息
Toast toast = Toast.makeText(MainActivity.this
, "簡單的提示信息"
// 設置該Toast提示信息的持續時間
, Toast.LENGTH_SHORT);
toast.show();
//或者Toast.makeText(MainActivity.this, "簡單的提示信息", Toast.LENGTH_SHORT).show()
}
});
Button bn = (Button) findViewById(R.id.bn);
// 為按鈕的單擊事件綁定事件監聽器
bn.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View source)
{
// 創建一個Toast提示信息
Toast toast = new Toast(MainActivity.this);
// 設置Toast的顯示位置
toast.setGravity(Gravity.CENTER, 0, 0);
// 創建一個ImageView
ImageView image = new ImageView(MainActivity.this);
image.setImageResource(R.drawable.tools);
// 創建一個LinearLayout容器
LinearLayout ll = new LinearLayout(MainActivity.this);
// 向LinearLayout中添加圖片、原有的View
ll.addView(image);
// 創建一個ImageView
TextView textView = new TextView(MainActivity.this);
textView.setText("帶圖片的提示信");
// 設置文本框內字體的大小和顏色
textView.setTextSize(30);
textView.setTextColor(Color.MAGENTA);
ll.addView(textView);
// 設置Toast顯示自定義View ,Toast里面可以設置View
toast.setView(ll);
// 設置Toast的顯示時間
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
});
}
}
~~~


- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite