Android控件开发之Toast

 Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。

 

 效果图

 

 

 本程序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">
<Button
    android:id="@+id/btn1" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn1"/>
<Button
    android:id="@+id/btn2" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn2" />
<Button
    android:id="@+id/btn3" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn3"/>
<Button
    android:id="@+id/btn4" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn4"/>
<Button
    android:id="@+id/btn5" 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/btn5" />

</LinearLayout>

 
java源码

package com.sx.Toast;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
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 ToastActivity extends Activity 
{
    /** Called when the activity is first created. */
    private Button btn1 = null;
    private Button btn2 = null;
    private Button btn3 = null;
    private Button btn4 = null;
    private Button btn5 = null;
 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        //获取button对象通过ID
        btn1 = (Button)findViewById(R.id.btn1);
        btn2 = (Button)findViewById(R.id.btn2);
        btn3 = (Button)findViewById(R.id.btn3);
        btn4 = (Button)findViewById(R.id.btn4);
        btn5 = (Button)findViewById(R.id.btn5);
        
        //设置button监听器
         btn1.setOnClickListener(new ButtonOnClicked());
        btn2.setOnClickListener(new ButtonOnClicked());
        btn3.setOnClickListener(new ButtonOnClicked());
        btn4.setOnClickListener(new ButtonOnClicked());
        btn5.setOnClickListener(new ButtonOnClicked());      
    }

    class  ButtonOnClicked implements OnClickListener 
    {
        //注:getApplicationContext() 方法是程序当前的默认Context
        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            Toast toast;
    
            switch(v.getId())
            {
            case R.id.btn1:
                Toast.makeText(getApplicationContext(), "默认Toast样式", Toast.LENGTH_LONG).show();
                break;    
            case R.id.btn2:
                toast = Toast.makeText(getApplicationContext(),"自定义位置Toast", Toast.LENGTH_LONG);
                toast.setGravity(Gravity.CENTER, 0, 0);
                toast.show();   
                break;    
            case R.id.btn3:
                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.btn4:
                break;  
            case R.id.btn5:
                break;
            }
        } 
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值