Toast

来源:互联网 发布:教育软件上市公司 编辑:程序博客网 时间:2024/06/03 19:02

Toast是一个提示语句,今天学习的是有关它的三个提示形式。

1.默认的提示。

2.在手机屏幕中间提示。

3.自定义Toast

首先创建一个ToastActivity

在activity_toast.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">    <Button        android:id="@+id/btn_toast_a"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="默认 "/>    <Button        android:id="@+id/btn_toast_b"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="改变位置 "/>    <Button        android:id="@+id/btn_toast_c"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="带图片(自定义)"/>    <Button        android:id="@+id/btn_toast_d"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="ToastUtil"/></LinearLayout>

接着在ToastActivity.java中写代码:

package net.nyist.lenovo.mytest;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.widget.Toast;import net.nyist.lenovo.mytest.util.ToastUtil;public class ToastActivity extends AppCompatActivity {    private Button mBtnToast1;    private Button mBtnToast2;    private Button mBtnToast3;    private Button mBtnToast4;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_toast);        mBtnToast1 = (Button) findViewById(R.id.btn_toast_a);        mBtnToast2 = (Button) findViewById(R.id.btn_toast_b);        mBtnToast3 = (Button) findViewById(R.id.btn_toast_c);        mBtnToast4 = (Button) findViewById(R.id.btn_toast_d);        Onclick onclick = new Onclick();        mBtnToast1.setOnClickListener(onclick);        mBtnToast2.setOnClickListener(onclick);        mBtnToast3.setOnClickListener(onclick);        mBtnToast4.setOnClickListener(onclick);    }    class Onclick implements View.OnClickListener{        @Override        public void onClick(View v) {            switch (v.getId()){                case R.id.btn_toast_a:                    Toast.makeText(ToastActivity.this, "Toast", Toast.LENGTH_SHORT).show();                    break;                case R.id.btn_toast_b:                    Toast toastCenter = Toast.makeText(ToastActivity.this, "Toast", Toast.LENGTH_SHORT);                    toastCenter.setGravity(Gravity.CENTER,0,0);                    //在手机屏幕中间显示提示                    toastCenter.show();                    break;                case R.id.btn_toast_c:                    Toast toastCustom = new Toast(getApplicationContext());                    //加载容器                    LayoutInflater inflater = LayoutInflater.from(ToastActivity.this);                    //将布局放到容器中                    View view = inflater.inflate(R.layout.layout_toast,null);                    //接下来获得到布局中的ImageView和TextView                    ImageView imageView = (ImageView) view.findViewById(R.id.iv_toast);                    TextView textView = (TextView)view.findViewById(R.id.tv_toast);                    //将图片放到ImageView中                    imageView.setImageResource(R.drawable.icon_smile);                    textView.setText("自定义Toast");                    toastCustom.setView(view);                    toastCustom.show();                    break;                case R.id.btn_toast_d:                    ToastUtil.showMsg(getApplicationContext(),"包装的Toast");                    break;            }        }    }}

这里强调一下自定义中的Toast,自定义的话,提示为一张图片,我们就像需要一个容器来把照片放到里面,首先需要准备给自定义的Toast准备一个布局layout_toast.xml
接着给他添加自定义的ImageView和TextView
代码如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:background="#99000000">    <LinearLayout        android:layout_width="200dp"        android:layout_height="200dp"        android:orientation="vertical"        android:gravity="center">        <ImageView            android:id="@+id/iv_toast"            android:layout_width="100dp"            android:layout_height="100dp"            android:scaleType="fitCenter"            />        <TextView            android:id="@+id/tv_toast"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:textSize="18sp"            android:textColor="@color/colorWhite"            android:layout_marginTop="10dp"            />    </LinearLayout></LinearLayout>

这里由于根布局不能够灵活运用,所以内嵌一个LinearLayout,设置ImageView的宽高都是100dp。

这里会发现还有第一个按钮,那就是和默认的那个有区别,什么区别呢?
默认的按钮我们点击十下的话,是从第一个提示一直等到第十个按钮一个一个显示完,一个两秒,这样的话就会长时间的得不到最新的结果,所以在这里我们不建议使用默认的Toast,在这里我们要想点击十下立即显示最后点击的结果,我们就需要封装一个Toast类来修改一下:

新建一个ToastUtil.java

package net.nyist.lenovo.mytest.util;import android.content.Context;import android.widget.Toast;/** * Created by LENOVO on 2017/9/8. */public class ToastUtil {    public static Toast mToast;    public static void showMsg(Context context,String msg){        if (mToast==null){            mToast = Toast.makeText(context,msg,Toast.LENGTH_SHORT);        }else {            mToast.setText(msg);        }        mToast.show();    }}

这样的话如果我们点击十下就会很快显示最后的结果。

 case R.id.btn_toast_d:                    ToastUtil.showMsg(getApplicationContext(),"包装的Toast");                    break;

在第四个case中调用这个类的方法,就可以实现了。

运行结果如下:
menu
default
center
diy