zoukankan      html  css  js  c++  java
  • Toast用法

    应用场景:弹出提示信息

    主界面:

    代码如下:

    复制代码
     @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            init();
        }
        private void init()
        {
            defaultToastBtn = (Button) findViewById(R.id.defaultToastBtn);
            customLocationBtn = (Button) findViewById(R.id.customLocationBtn);
            imageToastBtn = (Button) findViewById(R.id.imageToastBtn);
            customToastBtn = (Button) findViewById(R.id.customToastBtn);
            otherThreadBtn = (Button) findViewById(R.id.otherThreadBtn);
            
            defaultToastBtn.setOnClickListener(this);// 设置监听
            customLocationBtn.setOnClickListener(this);
            imageToastBtn.setOnClickListener(this);
            customToastBtn.setOnClickListener(this);
            otherThreadBtn.setOnClickListener(this);
        }
    复制代码

    1.默认样式的Toast

    代码如下:

    Toast.makeText(getApplicationContext(), "默认样式的Toast", Toast.LENGTH_SHORT).show();// 显示时间较短

    2.自定义位置的Toast

    代码如下:

    Toast toast = Toast.makeText(getApplicationContext(), "自定义位置 的Toast", Toast.LENGTH_LONG);//显示时间较长 
    toast.setGravity(Gravity.CENTER, 0, 0);// 居中显示
    toast.show();

    3.带图片的Toast

    代码如下:

    复制代码
    Toast toast = Toast.makeText(getApplicationContext(), "带图片的Toast", 3000);// 显示时间也可以是数字
    toast.setGravity(Gravity.TOP, 0, 0);// 最上方显示
    LinearLayout toastLayout = (LinearLayout) toast.getView();
    ImageView imageView = new ImageView(getApplicationContext());
    imageView.setImageResource(R.drawable.icon);
    toastLayout.addView(imageView, 0);// 0 图片在文字的上方 , 1 图片在文字的下方
    toast.show();
    复制代码

    4.完全自定义的Toast

    代码如下:

    复制代码
    LayoutInflater inflater = getLayoutInflater();// LayoutInflater对象
    View layout = inflater.inflate(R.layout.custom_view, null);
    ImageView imageView = (ImageView) layout.findViewById(R.id.imageView);
    TextView text = (TextView) layout.findViewById(R.id.textView);
    imageView.setImageResource(R.drawable.icon);
    text.setText("完全自定义的Toast");
    Toast toast = new Toast(getApplicationContext());
    // 底部 、水平居中,X偏移50 Y偏移50
    toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 50, 50);
    toast.setDuration(Toast.LENGTH_SHORT);
    toast.setView(layout);
    toast.show();
    复制代码

    5.来自其他线程的Toast

    代码如下:

    复制代码
    handler = new Handler();
    new Thread(new Runnable()
    {
        public void run()
            {
                show();
            }
    }).start();
    复制代码
    复制代码
    private void show()
    {
        handler.post(new Runnable()
        {
            @Override
            public void run()
    { Toast.makeText(getApplicationContext(), "Hello,I come from other thread!", 5000).show(); } }); }
    复制代码

    注:getApplicationContext()  表示它的生命周期是整个应用,应用摧毁它才被摧毁。

  • 相关阅读:
    第三方模块加载时出现XXX运行 提示错误:无法加载文件 C:UsersgxfAppDataRoaming pmXXX.ps1,因为在此系统上禁止运行脚本。
    node模块化
    前端面试集锦
    将字符转换成驼峰表示法
    JavaScript数据处理
    大数据基础整合
    移动端网页特效
    本地存储
    DOM重点核心
    触发器SQL
  • 原文地址:https://www.cnblogs.com/eternalisland/p/5600621.html
Copyright © 2011-2022 走看看