Android提醒方式之Toast

本文介绍了 Android 中 Toast 的使用方法,包括 makeText 方法的签名及参数介绍,并通过一个具体实例展示了如何在 Activity 中创建并显示 Toast。

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

概述

Toast是Android系统提供的一种提醒方式,在程序中可以使用它将一些短小的信息通知给用户,这些信息会在一段时间后自动消失,并且不会占用任何屏幕空间


makeText方法的签名及参数介绍
public class Toast {
  public static Toast makeText(Context context, CharSequence text, @Duration int duration)
  //Make a standard toast that just contains a text view.
  //@param context: The context to use
  //@param text: The text to show
  //@param duration: How long to display the message

   public static Toast makeText(Context context, @StringRes int resId, @Duration int duration)  throws Resources.NotFoundException
   //Make a standard toast that just contains a text view with the text from a resource.
   //@param context: The context to use
   //@param resId: The resource id of the string resource to use
   //@param duration: How long to display the message
   //@throw: If the resource can't be found
}

实例

public class FirstActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first_layout);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //通过Toast.makeText方法创建一个Toast对象,然后调用show()将其显示出来
                Toast.makeText(FirstActivity.this, "You click button 1", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

参考《第一行代码——Android》

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值