Android Toast五种用法

Android中提供一种简单的Toast消息提示框机制,可以在用户点击了某些按钮后,提示用户一些信息,提示的信息不能被用户点击,Toast的提示信息根据用户设置的显示时间后自动消失。Toast的提示信息可以在调试程序的时候方便的显示某些想显示的东西。

1、创建Toast:

Android Toast五种用法_第1张图片

用法一:

makeText(Context context, int resId, int duration) 

参数:contexttoast显示在哪个上下文,通常是当前ActivityresId指显示内容引用Resouce那条数据,就是从R类中去指定显示的消息内容;duration指定显示时间,Toast默认有LENGTH_SHORTLENGTH_LONG两常量,分别表示短时间显示和长时间显示

Toast.makeText(getApplicationContext(), "默认Toast样式",
Toast.LENGTH_SHORT).show();

用法二:

makeText(Context context, CharSequence text, int duration) 

参数:contextduration与第一个方法相同,参数text可以自己写消息内容。

用上面任意方法创建Toast对象之后调用方法show()即可显示。

Toast.makeText(getApplicationContext(), R.String.xxx,
Toast.LENGTH_SHORT).show();

2、设置Toast显示位置:

Android Toast五种用法_第2张图片

方法一:

setGravity(int gravity, int xOffset, int yOffset)

三个参数分别表示(起点位置,水平向右位移,垂直向下位移) 

Toast toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 200);

 方法二:

setMargin(float horizontalMargin, float verticalMargin)

 以横向和纵向的百分比设置显示位置,参数均为float类型(水平位移正右负左,竖直位移正上负下)

Toast toast.setMargin(-0.5f, 0f);

3、显示一个带图片的Toast效果:

Android Toast五种用法_第3张图片

toast = Toast.makeText(getApplicationContext(),
     "带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
//获得view对象
LinearLayout toastView = (LinearLayout) toast.getView();
实例化一个image对象
ImageView imageCodeProject = new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
//添加到view对象
toastView.addView(imageCodeProject, 0);
//显示
toast.show();

4、完全自定义效果

Android Toast五种用法_第4张图片

LayoutInflater inflater = getLayoutInflater();
    View layout = inflater.inflate(R.layout.custom,
    (ViewGroup) findViewById(R.id.llToast));
    ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);
    image.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();

5、其他线程:

Android Toast五种用法_第5张图片

new Thread(new Runnable() {
	public void run() {
		showToast();
	}
}).start();


6、完整代码:

1.   main.java
package com.wjq.toast;

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.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends Activity implements OnClickListener {
    Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        findViewById(R.id.btnSimpleToast).setOnClickListener(this);
        findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
                this);
        findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
        findViewById(R.id.btnCustomToast).setOnClickListener(this);
        findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);

    }

    public void showToast() {
        handler.post(new Runnable() {

            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), "我来自其他线程!",
                        Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onClick(View v) {
        Toast toast = null;
        switch (v.getId()) {
            case R.id.btnSimpleToast:
                Toast.makeText(getApplicationContext(), "默认Toast样式",
                        Toast.LENGTH_SHORT).show();
                break;
            case R.id.btnSimpleToastWithCustomPosition:
                toast = Toast.makeText(getApplicationContext(),
                        "自定义位置Toast", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();
                break;
            case R.id.btnSimpleToastWithImage:
                toast = Toast.makeText(getApplicationContext(),
                        "带图片的Toast", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                LinearLayout toastView = (LinearLayout) toast.getView();
                ImageView imageCodeProject = new ImageView(getApplicationContext());
                imageCodeProject.setImageResource(R.drawable.icon);
                toastView.addView(imageCodeProject, 0);
                toast.show();
                break;
            case R.id.btnCustomToast:
                LayoutInflater inflater = getLayoutInflater();
                View layout = inflater.inflate(R.layout.custom,
                        (ViewGroup) findViewById(R.id.llToast));
                ImageView image = (ImageView) layout
                        .findViewById(R.id.tvImageToast);
                image.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();
                break;
            case R.id.btnRunToastFromOtherThread:
                new Thread(new Runnable() {
                    public void run() {
                        showToast();
                    }
                }).start();
                break;

        }

    }
}
2.   main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
<Button android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
        android:text="默认"></Button>
<Button android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="自定义显示位置"
        android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
<Button android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
        android:text="带图片"></Button>
<Button android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="完全自定义"
        android:id="@+id/btnCustomToast"></Button>
<Button android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:text="其他线程"
        android:id="@+id/btnRunToastFromOtherThread"></Button>

</LinearLayout>
3.   custom.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" android:layout_width="wrap_content"
        android:background="#ffffffff" android:orientation="vertical"
        android:id="@+id/llToast" >
<TextView
android:layout_height="wrap_content"
        android:layout_margin="1dip"
        android:textColor="#ffffffff"
        android:layout_width="fill_parent"
        android:gravity="center"
        android:background="#bb000000"
        android:id="@+id/tvTitleToast" />
<LinearLayout
android:layout_height="wrap_content"
        android:orientation="vertical"
        android:id="@+id/llToastContent"
        android:layout_marginLeft="1dip"
        android:layout_marginRight="1dip"
        android:layout_marginBottom="1dip"
        android:layout_width="wrap_content"
        android:padding="15dip"
        android:background="#44000000" >
<ImageView
android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:id="@+id/tvImageToast" />
<TextView
android:layout_height="wrap_content"
        android:paddingRight="10dip"
        android:paddingLeft="10dip"
        android:layout_width="wrap_content"
        android:gravity="center"
        android:textColor="#ff000000"
        android:id="@+id/tvTextToast" />
</LinearLayout>
</LinearLayout>

 

你可能感兴趣的:(Android Toast五种用法)