Android实现Toast提示框图文并存的方法

2019-12-10 19:12:08丽君
易采站长站为您分析Android实现Toast提示框图文并存的方法,实例分析了Toast提示框的参数设置及图文调用的相关技巧,需要的朋友可以参考下  

本文实例讲述了Android实现Toast提示框图文并存的方法。,具体如下:

程序如下:

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.util.Linkify;
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.TextView;
import android.widget.Toast;
public class A05Activity extends Activity {
 private Button b;
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    b=(Button)findViewById(R.id.button);
    b.setText("显示Toast");
    b.setBackgroundColor(Color.GREEN);
    b.setOnClickListener(new OnClickListener(){
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  ImageView iv=new ImageView(A05Activity.this);
     iv.setImageResource(R.drawable.icon);
     TextView tv=new TextView(A05Activity.this);
     tv.setText(R.string.title);
     LinearLayout ll=new LinearLayout(A05Activity.this);
     //判断TextView中的内容是什么格式,并与系统连接
     Linkify.addLinks(tv, Linkify.PHONE_NUMBERS|Linkify.EMAIL_ADDRESSES|Linkify.WEB_URLS);
  Toast t=Toast.makeText(A05Activity.this, tv.getText(), Toast.LENGTH_LONG);
  View v1=t.getView();
  ll.setOrientation(LinearLayout.VERTICAL);
  ll.addView(iv);
  ll.addView(v1);
  t.setView(ll);
  //设置Toast对象在手机中的相对位置
  t.setGravity(Gravity.CENTER, 50, 50);
  t.show();
  }
    });
  }
}

注意:因为Toast属于Activiyt里的Context,所以在Toast里面的连接是无法单击的。比如在Toast中存在网址等内容是无法在Toast里面双击打开的。



注:相关教程知识阅读请移步到Android开发频道。