Android入門之Toast的使用教程
介紹
本篇帶來的是: Android用于提示信息的一個(gè)控件——Toast(吐司)!Toast是一種很方便的消息提示框,會(huì)在 屏幕中顯示一個(gè)消息提示框,沒任何按鈕,也不會(huì)獲得焦點(diǎn)一段時(shí)間過后自動(dòng)消失! 非常常用!我們通過一個(gè)例子把Toast的使用講透!
課程目標(biāo)
我們的目標(biāo)是實(shí)現(xiàn)2個(gè)Toast。
- 第一個(gè)Toast我們使用的是系統(tǒng)默認(rèn)的樣式,它顯示兩秒后自動(dòng)消失;
- 第二個(gè)Toast我們使用的是自定義的樣式,它在屏幕的中央顯示兩秒后自動(dòng)消失;
toast在屏幕上的閃現(xiàn)會(huì)有兩種Duration。
- Toast.LENGTH_SHORT,2秒;
- LENGTH_LONG,3點(diǎn)5秒;
項(xiàng)目結(jié)構(gòu)
我們會(huì)在自定義toast里使用一個(gè)圖片,我們把它放在mipmap下;
我們會(huì)使用一個(gè)自定義的toast,因此需要定義一個(gè)view_toast_custom.xml文件;
前端代碼
view_toast_custom.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/mkToast" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <ImageView android:id="@+id/toastBiaoQing" android:layout_width="24dp" android:layout_height="24dp" android:layout_marginLeft="10dp" android:src="@mipmap/wechat_goutou" /> <TextView android:id="@+id/toastMsg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:textSize="20sp" /> </LinearLayout>
activity_main.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" android:padding="5dp"> <Button android:id="@+id/btnShowToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="屏幕下部顯示2秒toast" /> <Button android:id="@+id/btnShowCustomToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="屏幕中部顯示2秒自定義toast" /> </LinearLayout>
后端代碼
MainActivity.java
package org.mk.android.demosimpletoast; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private Button btnShowToast; private Button btnShowCustomToast; private Context ctx; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ctx = MainActivity.this; btnShowToast = (Button) findViewById(R.id.btnShowToast); btnShowToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(MainActivity.this, "提示的內(nèi)容", Toast.LENGTH_SHORT).show(); } }); btnShowCustomToast = (Button) findViewById(R.id.btnShowCustomToast); btnShowCustomToast.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { midToast("這是一個(gè)自定義的toast",Toast.LENGTH_SHORT); } }); } private void midToast(String str, int showTime) { LayoutInflater inflater = getLayoutInflater(); View view = inflater.inflate(R.layout.view_toast_custom, (ViewGroup) findViewById(R.id.mkToast)); ImageView img_logo = (ImageView) view.findViewById(R.id.toastBiaoQing); TextView tv_msg = (TextView) view.findViewById(R.id.toastMsg); tv_msg.setText(str); Toast toast = new Toast(ctx); toast.setGravity(Gravity.CENTER, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(view); toast.show(); } }
動(dòng)手試試吧。
以上就是Android入門之Toast的使用教程的詳細(xì)內(nèi)容,更多關(guān)于Android Toast的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
RxJava和Retrofit2的統(tǒng)一處理單個(gè)請(qǐng)求示例詳解
這篇文章主要給大家介紹了關(guān)于RxJava和Retrofit2的統(tǒng)一處理單個(gè)請(qǐng)求的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11Android數(shù)據(jù)加密之Base64編碼算法的簡單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄狝ndroid數(shù)據(jù)加密之Base64編碼算法的簡單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-10-10Android實(shí)現(xiàn)單頁顯示3個(gè)Item的ViewPager炫酷切換效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)單頁顯示3個(gè)Item的ViewPager炫酷切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10Android View.onMeasure方法詳解及實(shí)例
這篇文章主要介紹了Android View.onMeasure方法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05Android不顯示開機(jī)向?qū)Ш烷_機(jī)氣泡問題
這篇文章主要介紹了Android不顯示開機(jī)向?qū)Ш烷_機(jī)氣泡問題,需要的朋友可以參考下2019-05-05Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程
這篇文章主要介紹了Android筆記設(shè)計(jì)范例之日記APP實(shí)現(xiàn)全流程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01解析Android中如何做到Service被關(guān)閉后又自動(dòng)啟動(dòng)的實(shí)現(xiàn)方法
本篇文章是對(duì)在Android中如何做到Service被關(guān)閉后又自動(dòng)啟動(dòng)的方法進(jìn)行了詳細(xì)的分析和介紹。需要的朋友參考下2013-05-05Android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能
本文給大家分享android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01