`
爪哇岛岛主
  • 浏览: 37881 次
  • 性别: Icon_minigender_1
  • 来自: 杭州(也就是天堂)
社区版块
存档分类
最新评论

android Toast5中表现形式

阅读更多
 
第一种,常规:
toast = Toast.makeText(HelloWorld.this, "Toast默认情况", Toast.LENGTH_SHORT);
toast.show();


第二种:自定义显示位置
toast = Toast.makeText(HelloWorld.this, "自定义显示位置", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();


第三种:带图片的Toast
toast = Toast.makeText(HelloWorld.this, "带图片的Toast", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView = (LinearLayout)toast.getView();
ImageView toastImage = new ImageView(HelloWorld.this);
toastImage.setImageResource(R.drawable.android2);
toastView.addView(toastImage,0);
toast.show();


第四种:完全自定义的toast
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.other,null);
toast = new Toast(HelloWorld.this);
toast.setGravity(Gravity.CENTER,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();


第五种:来自其他线程的Toast
Thread t = new Thread(OtherClasInstance);
t.start();


完整源代码:

package com.ymq.hello;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class HelloWorld extends Activity {
    private static Button b1 = null;
    private static Button b2 = null;
    private static Button b3 = null;
    private static Button b4 = null;
    private static Button b5 = null;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("activity has started...");
        setContentView(R.layout.main);
       
        b1 = (Button)this.findViewById(R.id.b1);
        b2 = (Button)this.findViewById(R.id.b2);
        b3 = (Button)this.findViewById(R.id.b3);
        b4 = (Button)this.findViewById(R.id.b4);
        b5 = (Button)this.findViewById(R.id.b5);
        
        myButtonListener bl = new myButtonListener();
        b1.setOnClickListener(bl);
        b2.setOnClickListener(bl);
        b3.setOnClickListener(bl);
        b4.setOnClickListener(bl);
        b5.setOnClickListener(bl);
    }
	
	class myButtonListener implements OnClickListener{
		Toast toast = null;
		public void onClick(View v) {
			if(v.equals(b1))
			{
				toast = Toast.makeText(HelloWorld.this, "Toast默认情况", Toast.LENGTH_SHORT);
				toast.show();
			}else if(v.equals(b2)){
				toast = Toast.makeText(HelloWorld.this, "自定义显示位置", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				toast.show();
			}else if(v.equals(b3)){
				toast = Toast.makeText(HelloWorld.this, "带图片的Toast", Toast.LENGTH_SHORT);
				toast.setGravity(Gravity.CENTER, 0, 0);
				LinearLayout toastView = (LinearLayout)toast.getView();
				ImageView toastImage = new ImageView(HelloWorld.this);
				toastImage.setImageResource(R.drawable.android2);
				toastView.addView(toastImage,0);
				toast.show();
			}else if(v.equals(b4)){
				LayoutInflater inflater = getLayoutInflater();
				View layout = inflater.inflate(R.layout.other,null);
				toast = new Toast(HelloWorld.this);
				toast.setGravity(Gravity.CENTER,0,0);
			    toast.setDuration(Toast.LENGTH_LONG);
			    toast.setView(layout);
			    toast.show();
			}else if(v.equals(b5)){
				Thread t = new Thread();
				t.start();
				toast = Toast.makeText(HelloWorld.this, "来自其他线程的Toast", Toast.LENGTH_SHORT);
				toast.show();
			}
		}
		
	}
}
  • 大小: 12.7 KB
  • 大小: 10.5 KB
  • 大小: 14.8 KB
  • 大小: 14.5 KB
分享到:
评论
1 楼 让安卓飞一会 2011-06-21  
我是来给岛主请安的~

相关推荐

Global site tag (gtag.js) - Google Analytics