• Android Studio [Toast]


    ToastActivity.java

    package com.xdw.a122;
    
    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 com.xdw.a122.util.ToastUtil;
    
    import java.util.zip.Inflater;
    
    public class ToastActivity extends AppCompatActivity {
        private Button mBtnToast1,mBtnToast2,mBtnToast3,mBtnToast4;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_toast);
            mBtnToast1=findViewById(R.id.btn_toast_1);
            mBtnToast2=findViewById(R.id.btn_toast_2);
            mBtnToast3=findViewById(R.id.btn_toast_3);
            mBtnToast4=findViewById(R.id.btn_toast_4);
            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_1:
                        Toast.makeText(getApplicationContext(),"Toast",Toast.LENGTH_LONG).show();
                        break;
                    case R.id.btn_toast_2:
                        Toast toastCenter =Toast.makeText(getApplicationContext(),"居中Toast",Toast.LENGTH_LONG);
                        toastCenter.setGravity(Gravity.CENTER,0,0);
                        toastCenter.show();
                        break;
                    case R.id.btn_toast_3:
                        Toast toastCustom =new Toast(getApplicationContext());
                        LayoutInflater inflater=LayoutInflater.from(ToastActivity.this);  //用来找res/layout/下的xml布局文件,并且实例化
                        View view=inflater.inflate(R.layout.layout_toast,null);     //layout_toast ---set size
                        ImageView imageView=view.findViewById(R.id.iv_toast);
                        TextView textView=view.findViewById(R.id.tv_toast);
                        imageView.setImageResource(R.drawable.back_2);          //选择显示的图片
                        textView.setText("自定义toast");
                        toastCustom.setView(view);
                        toastCustom.setDuration(Toast.LENGTH_LONG);
                        toastCustom.show();                                 //显示
                        break;
                    case R.id.btn_toast_4:
                        ToastUtil.showMsg(getApplicationContext(),"包装过的Toast");
                        break;
                }
            }
        }
    }

    activity_toast.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context=".ToastActivity"
        android:orientation="vertical">
        <Button
            android:id="@+id/btn_toast_1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="默认"/>
        <Button
            android:id="@+id/btn_toast_2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="改变位置"/>
        <Button
            android:id="@+id/btn_toast_3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="带图片"/>
        <Button
            android:id="@+id/btn_toast_4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="ToastUtil"/>
    
    </LinearLayout>

    封装的Toast

    ToastUtil.java

    package com.xdw.a122.util;
    
    import android.content.Context;
    import android.widget.Toast;
    
    import com.xdw.a122.ToastActivity;
    
    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_LONG);
            }
            else
                {
                    mToast.setText(msg);
                }
                mToast.show();
        }
    }
  • 相关阅读:
    多线程
    带缓存的输入输出流
    输入输出流I/O2
    输入输出流I/O
    课堂所讲整理:包装&工具类
    课堂所讲整理:Set和Map
    Java泛型和链表
    Java继承_接口练习题
    P235 实战练习(集合类)
    P188 实战练习(父类和子类)
  • 原文地址:https://www.cnblogs.com/zlc364624/p/10726772.html
Copyright © 2020-2023  润新知