android开发之Toast组件

package com.example.toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
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 implements OnClickListener{    
    private Button but1;
    private Button but2;
    private Button but3;
    private Button but4;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(this);
        Button b2=(Button)findViewById(R.id.button2);
        b2.setOnClickListener(this);
        Button b3=(Button)findViewById(R.id.button3);
        b3.setOnClickListener(this);
        Button b4=(Button)findViewById(R.id.button4);
        b4.setOnClickListener(this);
    }    
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.button1:
            toast1();
            break;
        case R.id.button2:
            toast2();
            break;
        case R.id.button3:
            toast3();
            break;
        case R.id.button4:
            toast4();
            break;    
        }
    }
    private void toast4() {
        // TODO Auto-generated method stub
        Toast t=new Toast(this);
        t.setGravity(Gravity.CENTER,0,0);
        
        ImageView iv=new ImageView(this);
        iv.setImageResource(R.drawable.ic_launcher);
        
        TextView tv=new TextView(this);
        tv.setText("图片信息");
        
        LinearLayout view=new LinearLayout(this);
        view.setOrientation(LinearLayout.HORIZONTAL);
        
        view.addView(iv);
        view.addView(tv);
        
        t.setView(view);
        t.show();
    }

    //图文提示信息
    private void toast3() {
        //构建Toast对象
        Toast t=Toast.makeText(this, "图片属性", Toast.LENGTH_LONG);
        t.setGravity(Gravity.CENTER, 0, 0);//设置显示位置
        //获取图片
        ImageView iv=new ImageView(this);
        iv.setImageResource(R.drawable.ic_launcher);
        //由Toast对象获取Layout对象;同学A向老师申请要和同学B一起去表演
        LinearLayout view =(LinearLayout)t.getView();//向老师申请
        view.setOrientation(LinearLayout.HORIZONTAL);//设置该组件布局方式
        //老师将B同学一同去表演   插入图片组件
        view.addView(iv,0);
        t.show();
    }
    //图片提示信息
    private void toast2() {
        // TODO Auto-generated method stub
        Toast t=new Toast(this);
        t.setDuration(Toast.LENGTH_LONG);//设置时间
        
        ImageView iv=new ImageView(this);
        iv.setImageResource(R.drawable.ic_launcher);
        //将某个组件放到指定组件显示。在指定组件类设置
        t.setView(iv);
        //设置位置
        t.setGravity(Gravity.CENTER|Gravity.TOP,0,0);
        t.show();
    }
    //文本提示信息
    private void toast1() {
        //
        Toast t=Toast.makeText(this,"文本提示",Toast.LENGTH_LONG);
        t.show();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值