信息提醒之Toast-更新中
概述
Toast與對話框類似,也會(huì)在屏幕的某個(gè)位置彈出一個(gè)窗口,在窗口中可以顯示文本、圖片等信息
與對話框不同的是,Toast信息提示框不可獲得焦點(diǎn),而且在顯示一定的時(shí)間后會(huì)自動(dòng)關(guān)閉。
因此,再顯示Toast信息提示框的同時(shí),屏幕上的控件仍然可以繼續(xù)操作。
Toast的基本用法
顯示Toast需要使用android.widget.Toast類。
只顯示文本的Toast
如果只是顯示文本的話,可以用如下代碼
Toast toast = Toast.makeText(this,"文字",Toast.LENGTH_LONG); toast.show();分析: 上述代碼使用Toast類的靜態(tài)方法創(chuàng)建了一個(gè)Toast對象。
該方法的第二個(gè)參數(shù)是要顯示的信息,
第三個(gè)參數(shù)標(biāo)識Toast提示信息顯示的時(shí)間。
由于Toast沒有按鈕,也無法通過手機(jī)按鍵關(guān)閉Toast,所以只能通過顯示時(shí)間的長短來控制Toast信息提示的時(shí)間自動(dòng)關(guān)閉。
Toast.LENGTH_LONG , Toast.LENGTH_SHORT .
注意:在創(chuàng)建只顯示文本的Toast對象時(shí),建議使用Toast.makeText方法,而不要直接new Toast對象,雖然Toast類有setText方法,但是不能在使用new關(guān)鍵字創(chuàng)建Toast對象后設(shè)置Toast提示信息框的文本信息。一下代碼會(huì)拋出異常
Toast toast = new Toast(); toast.setText("文字");// 此行代碼會(huì)拋出異常 toast.show();顯示文本和圖像的Toast- setView
// 將布局文件轉(zhuǎn)換為ViewView view = getLayoutInflater().inflate(R.layout.activity_custom_toast, null);// 設(shè)置提示文字TextView tv = (TextView) view.findViewById(R.id.textview);tv.setText("自定義Toast");// Toast展示Toast toast = new Toast(this);toast.setView(view);toast.setDuration(Toast.LENGTH_SHORT);toast.show();activity_custom_toast.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:layout_margin="10dp"android:background="#9AC0CD"android:orientation="horizontal" ><ImageView android:layout_width="50dp"android:layout_height="50dp"android:layout_gravity="center"android:src="@drawable/face" /><TextView android:id="@+id/textview"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_margin="10dp" /></LinearLayout>如果同時(shí)多個(gè)Toast信息提示框,系統(tǒng)會(huì)將這些Toast信息提示框放到隊(duì)列中,等前一個(gè)Toast信息提示框關(guān)閉后會(huì)顯示下一個(gè)Toast信息提示框,也就是說Toast信息提示框是按順序顯示的
用PopupWindow模擬Toast提示信息框
背景是.9的圖片
LayoutInflater inflater = LayoutInflater.from(this);View view = inflater.inflate(R.layout.activity_popupwd_toast, null);final PopupWindow popupWindow = new PopupWindow(view,500 ,200);popupWindow.setTouchable(false);popupWindow.showAtLocation(view, Gravity.CENTER_HORIZONTAL,20 ,0);// 設(shè)置定時(shí)器,5秒后自動(dòng)關(guān)閉android.os.Handler handler = new android.os.Handler();handler.postDelayed(new Runnable() {@Overridepublic void run() {popupWindow.dismiss();}} , 5*1000);activity_popupwd_toast.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#00FFFFFF"android:orientation="vertical"><TextView android:id="@+id/tvMsg"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/toast"android:text="有個(gè)定時(shí)器 設(shè)置的5秒后關(guān)閉...."android:textColor="#000"android:textSize="18sp" /> </LinearLayout>總結(jié)
以上是生活随笔為你收集整理的信息提醒之Toast-更新中的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android:descendantFo
- 下一篇: 信息提醒之对话框(AlertDialog