服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android实现Toast提示框图文并存的方法

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

2021-05-21 13:37马到成功 Android

这篇文章主要介绍了Android实现Toast提示框图文并存的方法,实例分析了Toast提示框的参数设置及图文调用的相关技巧,需要的朋友可以参考下

本文实例讲述了Android实现Toast提示框图文并存的方法。分享给大家供大家参考,具体如下:

程序如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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程序设计有所帮助。

延伸 · 阅读

精彩推荐