zoukankan      html  css  js  c++  java
  • android自定了了下Toast,随便玩玩

    好酒没写过博客了,今天无薪资休假一天,雅达,写点什么吧!

    最近公司来了几个三年经验的大犇,搞到我硬着头皮上去做他们的面试官,结果这个本来意气风发的面试官瞬间被秒得面目全非,

    我觉得我再也不会上了,感觉不会再爱了

    上头了吧!

    还是要秉承着中华民族的优良传统多写点博文,为国家,或者为大数据贡献一份力量,

    扯的有点远了,

    也不是什么高森的技术,只是设置一下Toast而已啦!随便改都可以的

    先上效果图

    上一个重写了Toast的代码,呢个系解决Toast不能及时显示的问题,到时候整合上面那个效果就OK啦啦!

    呢一个系可以直接使用的啦!

    /**
     * @author July
     * 
     * @version 1.0
     * @create 2013-6-14 下午8:47:58
     * 
     */
    public class JToast {
        
        // Toast实例
        private static Toast mToast;
        // 默认时长
        private static final int iStdDuration = Toast.LENGTH_SHORT;
        
        
        /**
         * @author July
         * @create 2013-6-14 下午9:02:29
         * 
         * @description:使用默认时长显示
         * 
         * @param context 
         * @param text 显示文本
         */
        public static void show(Context context, String text) {
            if (mToast == null) {
                mToast = Toast.makeText(context, text, iStdDuration);
            } else {
                mToast.setText(text);
            }
            mToast.show();
        }
        
        
        /**
         * @author July
         * @create 2013-6-14 下午9:02:49
         * 
         * @description:使用指定时长显示
         * 
         * @param context
         * @param text 显示文本
         * @param duration 显示时长
         */
        public static void show(Context context, String text, int duration) {
            if (mToast == null) {
                mToast = Toast.makeText(context, text, duration);
            } else {
                mToast.setText(text);
            }
            mToast.show();
        }
        
        /**
         * 
         * 
         * @author ddJuly
         * @date 2013-10-31 下午2:33:11
         * @param context
         * @param text
         */
        public static void show(Context context, int text) {
            if (mToast == null) {
                mToast = Toast.makeText(context, text, iStdDuration);
            } else {
                mToast.setText(text);
            }
            mToast.show();
        }
        
        
        /**
         * 
         * 
         * @author ddJuly
         * @date 2013-10-31 下午2:33:13
         * @param context
         * @param text
         * @param duration
         */
        public static void show(Context context, int text, int duration) {
            if (mToast == null) {
                mToast = Toast.makeText(context, text, duration);
            } else {
                mToast.setText(text);
            }
            mToast.show();
        }
        
    }

    一下呢个呢,就系那个效果图啦!

    MainActivity.java

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    LayoutInflater inflater = LayoutInflater
                            .from(MainActivity.this);
                    View view = inflater.inflate(R.layout.template_toast, null);
    
                    TextView textView = (TextView) view.findViewById(R.id.textView1);
    
                    SpannableString ss = new SpannableString("今天天气好吗?挺好的(网上抄的)");
    //                ss.setSpan(new ForegroundColorSpan(Color.RED), 0, 7,
    //                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    //                ss.setSpan(new ForegroundColorSpan(Color.GREEN), 7, 10,
    //                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    textView.setText(ss);
                    textView.setTextColor(Color.WHITE);
    
                    Toast toast = new Toast(MainActivity.this);
                    toast.setDuration(Toast.LENGTH_LONG);
                    toast.setView(view);
                    toast.setGravity(Gravity.TOP, 0, 100);
                    toast.show();
    
                }
            });
    
        }
    
    }

    toast_bg.xml

    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android" >
        <!-- 圆角 -->
        <corners
            android:radius="9dp"
            android:topLeftRadius="5dp"
            android:topRightRadius="5dp"
            android:bottomLeftRadius="5dp"
            android:bottomRightRadius="5dp"/><!-- 设置圆角半径 -->
        <!-- 渐变 -->
        <gradient />
        <!-- 间隔 -->
        <padding
            android:left="2dp"
            android:top="2dp"
            android:right="2dp"
            android:bottom="2dp"/><!-- 各方向的间隔 -->
        <!-- 大小 -->
        <size /><!-- 宽度和高度 -->
        <!-- 填充 -->
        <solid
            android:color="#90000000"/><!-- 填充的颜色 -->
        <!-- 描边 -->
        <stroke
            android:width="1dp"
            android:color="#90000000"
            android:dashWidth="0dp"
            android:dashGap="0dp"/>
    </shape>

    template_toast.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/toast_bg"
        android:gravity="center_horizontal"
        android:padding="12dp" >
    
        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="5dp" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/progressBar1"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_vertical"
            android:text="@string/hello_world"
            android:textColor="#FFFFFF" />
    
    </LinearLayout>

    好啦,全部代码上完啦!

    相信众叼毛ok的啦!!!

  • 相关阅读:
    Shiro权限验证
    5种设计模式整理
    多模块的SpringBoot项目
    Go使用数据库
    使用Go mod
    docker基本使用
    Go的IO操作
    实现一个网盘存储……
    Go的网络编程
    学习golang的历程
  • 原文地址:https://www.cnblogs.com/jinglingJuly/p/android_java.html
Copyright © 2011-2022 走看看