網站首頁 編程語言 正文
介紹
本篇帶來的是: 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
相關推薦
- 2022-02-13 C語言-操作符(詳細)和表達式求值
- 2022-08-30 AndroidStudio編譯報錯 Connect to repo.maven.apache.org
- 2022-12-01 Redis的數據存儲及String類型的實現_Redis
- 2022-09-07 解析react?函數組件輸入卡頓問題?usecallback?react.memo_React
- 2023-12-10 記錄一次多數據源配置失效的情況
- 2022-07-13 Linux下網絡配置和ifconfig命令的使用
- 2022-03-29 Qt超時鎖屏的實現示例_C 語言
- 2022-07-13 Springboot 項目中使用 @RestControllerAdvice 注解不生效
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支