android Toast5中表现形式

本文介绍了在Android开发中使用Toast的五种不同方法,包括默认显示、自定义位置、带图片、完全自定义视图及从其他线程调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

:cry: :oops:
第一种,常规:

toast = Toast.makeText(HelloWorld.this, "Toast默认情况", Toast.LENGTH_SHORT);
toast.show();


第二种:自定义显示位置

toast = Toast.makeText(HelloWorld.this, "自定义显示位置", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();


第三种:带图片的Toast

toast = Toast.makeText(HelloWorld.this, "带图片的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView toastImage = new ImageView(HelloWorld.this);
toastImage.setImageResource(R.drawable.android2);
toastView.addView(toastImage,0);
toast.show();


第四种:完全自定义的toast

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.other,null);
toast = new Toast(HelloWorld.this);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();


第五种:来自其他线程的Toast

Thread t = new Thread(OtherClasInstance);
t.start();


完整源代码:


package com.ymq.hello;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class HelloWorld extends Activity {
private static Button b1 = null;
private static Button b2 = null;
private static Button b3 = null;
private static Button b4 = null;
private static Button b5 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("activity has started...");
setContentView(R.layout.main);

b1 = (Button)this.findViewById(R.id.b1);
b2 = (Button)this.findViewById(R.id.b2);
b3 = (Button)this.findViewById(R.id.b3);
b4 = (Button)this.findViewById(R.id.b4);
b5 = (Button)this.findViewById(R.id.b5);

myButtonListener bl = new myButtonListener();
b1.setOnClickListener(bl);
b2.setOnClickListener(bl);
b3.setOnClickListener(bl);
b4.setOnClickListener(bl);
b5.setOnClickListener(bl);
}

class myButtonListener implements OnClickListener{
Toast toast = null;
public void onClick(View v) {
if(v.equals(b1))
{
toast = Toast.makeText(HelloWorld.this, "Toast默认情况", Toast.LENGTH_SHORT);
toast.show();
}else if(v.equals(b2)){
toast = Toast.makeText(HelloWorld.this, "自定义显示位置", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}else if(v.equals(b3)){
toast = Toast.makeText(HelloWorld.this, "带图片的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView toastImage = new ImageView(HelloWorld.this);
toastImage.setImageResource(R.drawable.android2);
toastView.addView(toastImage,0);
toast.show();
}else if(v.equals(b4)){
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.other,null);
toast = new Toast(HelloWorld.this);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}else if(v.equals(b5)){
Thread t = new Thread();
t.start();
toast = Toast.makeText(HelloWorld.this, "来自其他线程的Toast", Toast.LENGTH_SHORT);
toast.show();
}
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值