zoukankan      html  css  js  c++  java
  • Android Toast 使用总结

    本文内容

    • 环境
    • 演示 Toast 使用

    环境


    • Windows 2008 R2 64 位
    • Eclipse ADT V22.6.2,Android 4.4.3
    • 三星 SM-G3508,Android OS 4.1

    演示 Toast 使用


    渐进演示如何使用 Toast。

    123

    图 1 左:主程序;中:点击“默认 Toast”;右:点击“自定义位置 Toast”

    456

    图 2 左:点击“带图片 Toast”;中:点击“自定义 Toast”;右:“来自其他线程 Toast”

    main.xml 文件,只是六个按钮;Toast 也可以将 custom.xml 文件作为信息提示,具体下载 Demo,此处略,核心代码瑞安所示:

    package com.example.toastdemo.activity;
     
    import com.example.toastdemo.R;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.os.Handler;
    import android.view.Gravity;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.LinearLayout;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class MainActivity extends Activity {
        Handler handler = new Handler();
        Toast toast = null;
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
     
            findViewById(R.id.btnSimpleToast).setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(getApplicationContext(), "默认Toast",
                                    Toast.LENGTH_SHORT).show();
                        }
                    });
     
            findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            toast = Toast.makeText(getApplicationContext(),
                                    "自定义位置Toast", Toast.LENGTH_LONG);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                        }
                    });
     
            findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            toast = Toast.makeText(getApplicationContext(),
                                    "带图片Toast", Toast.LENGTH_LONG);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            LinearLayout toastView = (LinearLayout) toast.getView();
                            ImageView image1 = new ImageView(
                                    getApplicationContext());
                            image1.setImageResource(R.drawable.icon);
                            toastView.addView(image1, 0);
                            toast.show();
                        }
                    });
     
            findViewById(R.id.btnCustomToast).setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            LayoutInflater inflater = getLayoutInflater();
                            View layout = inflater.inflate(R.layout.custom,
                                    (ViewGroup) findViewById(R.id.llToast));
                            ImageView image2 = (ImageView) layout
                                    .findViewById(R.id.tvImageToast);
                            image2.setImageResource(R.drawable.icon);
                            TextView title = (TextView) layout
                                    .findViewById(R.id.tvTitleToast);
                            title.setText("Attention");
                            TextView text = (TextView) layout
                                    .findViewById(R.id.tvTextToast);
                            text.setText("自定义Toast");
                            toast = new Toast(getApplicationContext());
                            toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
                            toast.setDuration(Toast.LENGTH_LONG);
                            toast.setView(layout);
                            toast.show();
                        }
                    });
     
            findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            new Thread(new Runnable() {
                                public void run() {
                                    showToast();
                                }
                            }).start();
                        }
                    });
     
        }
     
        public void showToast() {
            handler.post(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(), "来自其他线程Toast",
                            Toast.LENGTH_SHORT).show();
     
                }
            });
        }
    }

    下载 Demo

  • 相关阅读:
    【转】浏览器兼容性问题汇总
    【转】sql server数据库操作大全——常用语句/技巧集锦/经典语句
    如何在数据库中导入excel文件内的数据
    【总算解决了】A network-related or instance-specific error occurred while establishing a connection to SQL Server
    【转】JS容器拖拽效果,并通过cookie保存拖拽各容器的所在位置
    【转】SQL多条件模糊查询解决方案-存储过程
    ASP搜索查询
    解决SQL Server 阻止了对组件 'Ad Hoc Distributed Queries' 的 STATEMENT 'OpenRowset/OpenDatasource' 的访问
    简单鼠标跟随代码
    【JS】jquery通知插件toastr
  • 原文地址:https://www.cnblogs.com/liuning8023/p/3798338.html
Copyright © 2011-2022 走看看