android取消toast_android重写toast

android取消toast_android重写toast本文实例讲述了AndroidToast通知用法。分享给大家供大家参考,具体如下:Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。1.默认用法Toast.makeText(getApplicationContext(),”默认Toast样式”,Toast.LENGTH_SHORT).show();2.Fragment中的用法Toast.makeText(getActivity…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

本文实例讲述了Android Toast通知用法。分享给大家供大家参考,具体如下:

Toast在手机屏幕上向用户显示一条信息,一段时间后信息会自动消失。

1.默认用法

Toast.makeText(getApplicationContext(), “默认Toast样式”,Toast.LENGTH_SHORT).show();

2.Fragment中的用法

Toast.makeText(getActivity(),”网络连接错误,请检察网络设置”, Toast.LENGTH_LONG).show();

3.自定义显示位置效果

toast = Toast.makeText(getApplicationContext(), “自定义位置Toast”, Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

4.带图片效果

toast = Toast.makeText(getApplicationContext(), “带图片的Toast”, Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

LinearLayout toastView = (LinearLayout) toast.getView();

ImageView imageCodeProject = new ImageView(getApplicationContext());

imageCodeProject.setImageResource(R.drawable.icon);

toastView.addView(imageCodeProject, 0);

toast.show();

5.完全自定义效果

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));

ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);

image.setImageResource(R.drawable.icon);

TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);

title.setText(“Attention”);

TextView text = (TextView) layout.findViewById(R.id.tvTextToast);

text.setText(“完全自定义Toast”);

toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.show();

6.其他线程

Main.java代码:

package com.wjq.toast;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.view.Gravity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.view.View.OnClickListener;

import android.widget.ImageView;

import android.widget.LinearLayout;

import android.widget.TextView;

import android.widget.Toast;

public class Main extends Activity implements OnClickListener {

Handler handler = new Handler();

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

findViewById(R.id.btnSimpleToast).setOnClickListener(this);

findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(this);

findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);

findViewById(R.id.btnCustomToast).setOnClickListener(this);

findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);

}

public void showToast() {

handler.post(new Runnable() {

@Override

public void run() {

Toast.makeText(getApplicationContext(), “我来自其他线程!”,Toast.LENGTH_SHORT).show();

}

});

}

@Override

public void onClick(View v) {

Toast toast = null;

switch (v.getId()) {

case R.id.btnSimpleToast:

Toast.makeText(getApplicationContext(), “默认Toast样式”,Toast.LENGTH_SHORT).show();

break;

case R.id.btnSimpleToastWithCustomPosition:

toast = Toast.makeText(getApplicationContext(), “自定义位置Toast”,Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

break;

case R.id.btnSimpleToastWithImage:

toast = Toast.makeText(getApplicationContext(), “带图片的Toast”,Toast.LENGTH_LONG);

toast.setGravity(Gravity.CENTER, 0, 0);

LinearLayout toastView = (LinearLayout) toast.getView();

ImageView imageCodeProject = new ImageView(getApplicationContext());

imageCodeProject.setImageResource(R.drawable.icon);

toastView.addView(imageCodeProject, 0);

toast.show();

break;

case R.id.btnCustomToast:

LayoutInflater inflater = getLayoutInflater();

View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.llToast));

ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);

image.setImageResource(R.drawable.icon);

TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);

title.setText(“Attention”);

TextView text = (TextView) layout.findViewById(R.id.tvTextToast);

text.setText(“完全自定义Toast”);

toast = new Toast(getApplicationContext());

toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);

toast.setDuration(Toast.LENGTH_LONG);

toast.setView(layout);

toast.show();

break;

case R.id.btnRunToastFromOtherThread:

new Thread(new Runnable() {

public void run() {

showToast();

}

}).start();

break;

}

}

}

main.xml代码:

android:layout_width=”fill_parent”

android:layout_height=”fill_parent”

android:gravity=”center”

android:orientation=”vertical”

android:padding=”5dip” >

android:id=”@+id/btnSimpleToast”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”默认” >

android:id=”@+id/btnSimpleToastWithCustomPosition”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”自定义显示位置” >

android:id=”@+id/btnSimpleToastWithImage”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”带图片” >

android:id=”@+id/btnCustomToast”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”完全自定义” >

android:id=”@+id/btnRunToastFromOtherThread”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:text=”其他线程” >

custom.xml:

android:id=”@+id/llToast”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:background=”#ffffffff”

android:orientation=”vertical” >

android:id=”@+id/tvTitleToast”

android:layout_width=”fill_parent”

android:layout_height=”wrap_content”

android:layout_margin=”1dip”

android:background=”#bb000000″

android:gravity=”center”

android:textColor=”#ffffffff” />

android:id=”@+id/llToastContent”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_marginBottom=”1dip”

android:layout_marginLeft=”1dip”

android:layout_marginRight=”1dip”

android:background=”#44000000″

android:orientation=”vertical”

android:padding=”15dip” >

android:id=”@+id/tvImageToast”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:layout_gravity=”center” />

android:id=”@+id/tvTextToast”

android:layout_width=”wrap_content”

android:layout_height=”wrap_content”

android:gravity=”center”

android:paddingLeft=”10dip”

android:paddingRight=”10dip”

android:textColor=”#ff000000″ />

希望本文所述对大家Android程序设计有所帮助。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/194402.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python button使用方法_python gui界面设计

    python button使用方法_python gui界面设计Button控件是一种标准Tkinter控件,用来展现不同样式的按钮.Button控件被用以和用户交互,比如按钮被鼠标点击后,某种操作被启动.和Label控件类似,按钮可以展示图片或者文字.不同的是,Label控件可以指定字体,Button控件只能使用单一的字体.Button上的文字可以多行显示.可以将一个Python函数或方法绑定到一个B

    2022年8月30日
    4
  • MyEclipse每次修改js有另一种部署

    MyEclipse每次修改js有另一种部署

    2022年1月2日
    45
  • Python面试中常见的40个问题

    Python面试中常见的40个问题:(1)什么是Python?使用Python有什么好处?(2)什么是PEP 8?(3)什么是序列化和非序列化?(4)如何解释Python?(5)如何在Python中进行内存管理,等等。

    2022年1月18日
    89
  • 解决Qt程序在Linux下无法输入中文的办法

    解决Qt程序在Linux下无法输入中文的办法

    2021年12月10日
    49
  • plc上位机软件编程_有上位机还必须用plc吗

    plc上位机软件编程_有上位机还必须用plc吗1、PLC的发展历程在工业生产过程中,大量的开关量顺序控制,它按照逻辑条件进行顺序动作,并按照逻辑关系进行连锁保护动作的控制,及大量离散量的数据采集。传统上,这些功能是通过气动或电气控制系统来实现的。1968年美国GM(通用汽车)公司提出取代继电气控制装置的要求,第二年,美国数字公司研制出了基于集成电路和电子技术的控制装置,首次采用程序化的手段应用于电气控制,这就是代可编程序控制器,称Progra…

    2025年10月1日
    3
  • 如何在linux中安装VMwareTools

    如何在linux中安装VMwareToolsvmwaretools是虚拟机VMwareWorkstation自带的一款工具,它的作用就是使用户可以从物理主机直接往虚拟机里面拖文件。如果不安装它,我们是无法进行虚拟机和物理机之间的文件传输的,当然它的功效不止于此,平时我们操作虚拟机的时候,在物理机和虚拟机之间必须使用CTRL切换,如果安装之后我们就不必使用键盘切换,直接便可退出,使得虚拟机真正成为了电脑的一部分,那么这么神奇的vmware

    2022年5月25日
    41

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号