Android使用Toast顯示消息提示框
在前面的實例中,已經(jīng)應用過Toast類來顯示一個簡單的提示框了。這次將對Toast進行詳細介紹。Toast類用于在屏幕中顯示一個消息提示框,該消息提示框沒有任何控制按鈕,并且不會獲得焦點,經(jīng)過一段時間后自動消失。通常用于顯示一些快速提示信息,應用范圍非常廣泛。
使用Toast來顯示消息提示框非常簡單,只需要一下三個步驟:
(1).創(chuàng)建一個Toast對象。通常有兩種方法:一種是使用構造方式進行創(chuàng)建;
Toast toast=new Toast(this);
另一種是調用Toast類的makeText()方法創(chuàng)建。
Toast toast=Toast.makeText(this,"要顯示的內容",Toast.LENGTH_SHORT);
(2).調用Toast類提供的方法來設置該消息提示框的對齊方式、頁邊距、顯示的內容等等。
常用的方法如下:
setDuration(int duration) 用于設置消息提示框持續(xù)的時間,參數(shù)通常使用Toast.LENGTH_LONG或Toast.LENGTH_SHORT
setGravity(int gravity,int xOffset,int yOffset) 用于設置消息提示框的位置,參數(shù)grivaty用于指定對齊方式:xOffset和yOffset用于指定具體的偏移值
setMargin(float horizontalMargin,float verticalMargin) 用于設置消息提示的頁邊距
setText(CharSequence s) 用于設置要顯示的文本內容
setView(View view) 用于設置將要在提示框中顯示的視圖
(3).調用Toast類的show()方法顯示消息提示框。需要注意的是,一定要調用該方法,否則設置的消息提示框將不顯示。
下面通過一個具體的實例來說明如何使用Toast類顯示消息提示框。
res/layout/main.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/layout1" android:gravity="center_horizontal" > </LinearLayout>
MainActivity:
package com.example.test; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; 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.main); //通過makeText方法創(chuàng)建消息提示框 Toast.makeText(MainActivity.this, "我是通過makeText方法創(chuàng)建的消息提示框", Toast.LENGTH_SHORT).show(); //通過Toast類的構造方法創(chuàng)建消息提示框 Toast toast=new Toast(this); toast.setDuration(Toast.LENGTH_SHORT);//設置持續(xù)時間 toast.setGravity(Gravity.CENTER,0, 0);//設置對齊方式 LinearLayout ll=new LinearLayout(this);//創(chuàng)建一個線性布局管理器 ImageView imageView=new ImageView(this); imageView.setImageResource(R.drawable.stop); imageView.setPadding(0, 0, 5, 0); ll.addView(imageView); TextView tv=new TextView(this); tv.setText("我是通過構造方法創(chuàng)建的消息提示框"); ll.addView(tv); toast.setView(ll);//設置消息提示框中要顯示的視圖 toast.show();//顯示消息提示框 } }
效果如圖:
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
CoordinatorLayout的使用如此簡單(Android)
這篇文章主要為大家詳細介紹了Android CoordinatorLayout的使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Kotlin Select協(xié)程多路復用的實現(xiàn)詳解
select是Kotlin 1.6中的特性,即選擇最快的結果。select與async、Channel結合使用,可以大大提高程序的響應速度,還可以提高程序的靈活性、擴展性2022-09-09android組件SwipeRefreshLayout下拉小球式刷新效果
這篇文章主要為大家詳細介紹了android組件SwipeRefreshLayout下拉小球式刷新效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-02-02EditText限制輸入數(shù)字,精確到小數(shù)點后1位的設置方法
下面小編就為大家?guī)硪黄狤ditText限制輸入數(shù)字,精確到小數(shù)點后1位的設置方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04Android之ArcSlidingHelper制作圓弧滑動效果
這篇文章主要介紹了Android之ArcSlidingHelper制作圓弧滑動效果,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-08-08詳解Android App中的AsyncTask異步任務執(zhí)行方式
這篇文章主要介紹了Android App中的AsyncTask異步任務執(zhí)行方式,文中舉了一個打開網(wǎng)絡圖片的例子幫助大家直觀理解,需要的朋友可以參考下2016-04-04