Android Toast 使用总结
本文内容
- 环境
- 演示 Toast 使用
环境
- Windows 2008 R2 64 位
- Eclipse ADT V22.6.2,Android 4.4.3
- 三星 SM-G3508,Android OS 4.1
演示 Toast 使用
渐进演示如何使用 Toast。
图 1 左:主程序;中:点击“默认 Toast”;右:点击“自定义位置 Toast”
图 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();
}
});
}
}