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)文章
Android12四大組件之Activity生命周期變化詳解
雖然說我們天天都在使用Activity,但是你真的對(duì)Activity的生命機(jī)制完全了解了嗎?Activity的生命周期方法只有七個(gè),但是其實(shí)那只是默認(rèn)的情況。也就是說在其他情況下,Activity的生命周期可能不會(huì)是按照我們以前所知道的流程,本章著重講解Activity的生命周期變化2022-07-07
Android自定義水波紋動(dòng)畫Layout實(shí)例代碼
這篇文章主要介紹了Android自定義水波紋動(dòng)畫Layout的實(shí)例代碼,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-11-11
Android 開機(jī)充電圖標(biāo)和充電動(dòng)畫效果
這篇文章主要介紹了Android 開機(jī)充電圖標(biāo)和充電動(dòng)畫效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12
簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序 附源碼
這篇文章主要幫助大家簡(jiǎn)單實(shí)現(xiàn)Android鬧鐘程序,附源碼下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Flutter?App開發(fā)實(shí)現(xiàn)循環(huán)語(yǔ)句的方式實(shí)例
這篇文章主要為大家介紹了Flutter?App開發(fā)實(shí)現(xiàn)循環(huán)語(yǔ)句的方式示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-05-05
Android實(shí)現(xiàn)圖片的高斯模糊(兩種方式)
本文給大家分享兩種實(shí)現(xiàn)圖片的高斯模糊效果,非常不錯(cuò),具有參考借鑒價(jià)值,對(duì)android圖片高斯模糊效果感興趣的朋友一起看看吧2017-03-03
利用adt-bundle輕松搭建Android開發(fā)環(huán)境與Hello world(Windows)
這篇文章主要介紹了利用adt-bundle在Windows下輕松搭建Android開發(fā)環(huán)境與Hello world,感興趣的小伙伴們可以參考一下2016-07-07
Android畢業(yè)設(shè)計(jì)備忘錄APP
這篇文章主要介紹了一個(gè)Android畢業(yè)設(shè)計(jì)備忘錄APP,它很小,但是功能很全,可實(shí)現(xiàn)添加、刪除、修改、查看的功能,使用Java語(yǔ)言開發(fā),風(fēng)格簡(jiǎn)練2021-08-08
Android app啟動(dòng)時(shí)黑屏或者白屏的原因及解決辦法
這篇文章主要介紹了Android app啟動(dòng)時(shí)黑屏或者白屏的原因及解決辦法的相關(guān)資料,需要的朋友可以參考下2016-09-09

