日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android入門之Toast的使用教程_Android

作者:TGITCIC ? 更新時間: 2022-12-15 編程語言

介紹

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

課程目標

我們的目標是實現2個Toast。

  • 第一個Toast我們使用的是系統默認的樣式,它顯示兩秒后自動消失;
  • 第二個Toast我們使用的是自定義的樣式,它在屏幕的中央顯示兩秒后自動消失;

toast在屏幕上的閃現會有兩種Duration。

  • Toast.LENGTH_SHORT,2秒;
  • LENGTH_LONG,3點5秒;

項目結構

我們會在自定義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, "提示的內容", 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();
    }
}

動手試試吧。

原文鏈接:https://blog.csdn.net/lifetragedy/article/details/127926384

欄目分類
最近更新