欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android入門之Toast的使用教程

 更新時(shí)間:2022年11月19日 09:30:56   作者:TGITCIC  
Toast是一種很方便的消息提示框,會(huì)在 屏幕中顯示一個(gè)消息提示框,沒任何按鈕,也不會(huì)獲得焦點(diǎn)一段時(shí)間過后自動(dòng)消失!非常常用!本文就來通過一個(gè)例子把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)文章

最新評(píng)論