Android入門之Toast的使用教程
介紹
本篇帶來的是: Android用于提示信息的一個控件——Toast(吐司)!Toast是一種很方便的消息提示框,會在 屏幕中顯示一個消息提示框,沒任何按鈕,也不會獲得焦點一段時間過后自動消失! 非常常用!我們通過一個例子把Toast的使用講透!
課程目標

我們的目標是實現(xiàn)2個Toast。
- 第一個Toast我們使用的是系統(tǒng)默認的樣式,它顯示兩秒后自動消失;
- 第二個Toast我們使用的是自定義的樣式,它在屏幕的中央顯示兩秒后自動消失;
toast在屏幕上的閃現(xiàn)會有兩種Duration。
- Toast.LENGTH_SHORT,2秒;
- LENGTH_LONG,3點5秒;
項目結(jié)構(gòu)

我們會在自定義toast里使用一個圖片,我們把它放在mipmap下;
我們會使用一個自定義的toast,因此需要定義一個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("這是一個自定義的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();
}
}動手試試吧。
以上就是Android入門之Toast的使用教程的詳細內(nèi)容,更多關(guān)于Android Toast的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
RxJava和Retrofit2的統(tǒng)一處理單個請求示例詳解
這篇文章主要給大家介紹了關(guān)于RxJava和Retrofit2的統(tǒng)一處理單個請求的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2018-11-11
Android數(shù)據(jù)加密之Base64編碼算法的簡單實現(xiàn)
下面小編就為大家?guī)硪黄狝ndroid數(shù)據(jù)加密之Base64編碼算法的簡單實現(xiàn)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-10-10
Android實現(xiàn)單頁顯示3個Item的ViewPager炫酷切換效果
這篇文章主要為大家詳細介紹了Android實現(xiàn)單頁顯示3個Item的ViewPager炫酷切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android筆記設計范例之日記APP實現(xiàn)全流程
這篇文章主要介紹了Android筆記設計范例之日記APP實現(xiàn)全流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2023-01-01
解析Android中如何做到Service被關(guān)閉后又自動啟動的實現(xiàn)方法
本篇文章是對在Android中如何做到Service被關(guān)閉后又自動啟動的方法進行了詳細的分析和介紹。需要的朋友參考下2013-05-05
Android開發(fā)中RecyclerView模仿探探左右滑動布局功能
本文給大家分享android開發(fā)中RecyclerView模仿探探左右滑動布局功能,非常不錯,具有參考借鑒價值,需要的朋友參考下2017-01-01

