Android Toast的基本使用

Android Toast的基本使用

一、 说明

本文主要讲一下Android Toast的基本使用。

二、 所用工具

Android Studio

三、 具体内容

Toast:
主要用来提示信息
语法:
Toast.makeText(当前上下文, “要提示的内容”, 显示的时长).show()
例子:
Xml代码:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
    <Button
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="显示"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>

界面效果图:
在这里插入图片描述
Activity代码:

public class MainActivity extends AppCompatActivity {
    Activity mActivityContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mActivityContext=this;
        Button btnSubmit  = findViewById(R.id.btn_show);
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                midToast("显示内容",Toast.LENGTH_LONG);
            }
        });
    }
    //我这里为了方便提取出来写了一个方法
    public void midToast(String str,int showTime)
    {
        Toast toast = Toast.makeText(mActivityContext, str, showTime);
        //设置Toast显示的位置
        toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL,100,100);
        //获取组件
        TextView v =  toast.getView().findViewById(android.R.id.message);
        //设置Toast的字体颜色
        v.setTextColor(Color.BLACK);
        toast.show();
    }
}

点击按钮后的效果图:在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值