码迷,mamicode.com
首页 > 移动开发 > 详细

Android Toast

时间:2014-06-28 14:32:28      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   color   get   

本文内容

  • 环境
  • 演示 Toast 使用

环境


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

 

演示 Toast 使用


渐进演示如何使用 Toast。

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

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

bubuko.com,布布扣bubuko.com,布布扣bubuko.com,布布扣

图 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

Android Toast,布布扣,bubuko.com

Android Toast

标签:android   style   blog   http   color   get   

原文地址:http://www.cnblogs.com/liuning8023/p/3798338.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!