本文实例讲述了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程序设计有所帮助。