入博第一篇—Toast显示时间以及样式的自定义

大家好,这是我入博后的第一篇个人技术博客,在写之前也是参考了一些资料,但是内容绝对真实,出自本人之手,希望在这个平台跟大家一起交流学习,共同成长。

好了,话不多说,开始进入主题,今天我要说的Toast,首先,Toast是一个视图,可以看得见,但是摸不着,为什么说摸不着呢,因为Toast是没有焦点的,也就是说Toast只是一个简单的展示提示信息的弹窗,它并不能像Dialog可以对其进行点击处理,那么如果是系统默认的Toast的话,一般情况下会以文本的方式显示在屏幕的下方,而且只会保持几秒的时间的就会自动消失,但是这样简单的样式有时候并不能满足现实开发当中复杂的需求。接下来,我要说的是对于Toast的显示内容跟弹出来的位置以及显示时间的自定义。再犀利点语言也抵不过图片来的简单粗暴直观,那下面直接上截图:

1.主页面三个功能点的点击按钮:

这个就不用多说;

 

2.系统默认Toast的显示的位置:

 

3.自定义Toast样式的显示:

Toast显示的内容有文本,有图片,而且显示位置可以根据需求自己制定;

 

4.自定义Toast显示的时间:

 

下面的实现上面效果的代码:

MainActivity:

 1 import java.util.Timer;
 2 import java.util.TimerTask;
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Gravity;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.view.ViewGroup;
10 import android.widget.ImageView;
11 import android.widget.TextView;
12 import android.widget.Toast;
13 
14 public class MainActivity extends Activity {
15 
16     private TextView tv1;
17     private TextView tv2;
18     private TextView tv3;
19 
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         tv1 = (TextView) findViewById(R.id.tv_1);
25         tv2 = (TextView) findViewById(R.id.tv_2);
26         tv3 = (TextView) findViewById(R.id.tv_3);
27         tv1.setOnClickListener(new OnClickListener() {
28 
29             @Override
30             public void onClick(View v) {
31                 DefaultToast();
32 
33             }
34 
35         });
36         tv2.setOnClickListener(new OnClickListener() {
37 
38             @Override
39             public void onClick(View v) {
40                 CustomStylesToast();
41 
42             }
43 
44         });
45         tv3.setOnClickListener(new OnClickListener() {
46 
47             @Override
48             public void onClick(View v) {
49                 CustomTimeToast();
50 
51             }
52 
53         });
54     }
55 
56     private void DefaultToast() {
57         Toast.makeText(this, "Hello", Toast.LENGTH_SHORT).show();
58 
59     }
60 
61     private void CustomStylesToast() {
62         LayoutInflater inflater = getLayoutInflater();
63         View layout = inflater.inflate(R.layout.toast_custom_styles,
64                 (ViewGroup) findViewById(R.id.ll_Toast));
65         ImageView image = (ImageView) layout.findViewById(R.id.iv_ImageToast);
66         image.setImageResource(R.drawable.ic_launcher);
67         TextView text = (TextView) layout.findViewById(R.id.tv_TextToast);
68         text.setText("自定义Toast的样式");
69         Toast toast = new Toast(this);
70         toast.setGravity(Gravity.LEFT | Gravity.TOP, 20, 50);
71         toast.setDuration(Toast.LENGTH_LONG);
72         toast.setView(layout);
73         toast.show();
74 
75     }
76 
77     private void CustomTimeToast() {
78         final Toast toast = Toast.makeText(this, "自定义Toast的时间",
79                 Toast.LENGTH_LONG);
80         final Timer timer = new Timer();
81         timer.schedule(new TimerTask() {
82             @Override
83             public void run() {
84                 toast.show();
85             }
86         }, 0, 3000);// 3000表示点击按钮之后,Toast延迟3000ms后显示
87         new Timer().schedule(new TimerTask() {
88             @Override
89             public void run() {
90                 toast.cancel();
91                 timer.cancel();
92             }
93         }, 5000);// 5000表示Toast显示时间为5秒
94 
95     }
96 }

activity_main.xml 与 toast_custom_styles.xml :

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     tools:context="${relativePackage}.${activityClass}" >
 7 
 8     <TextView
 9         android:id="@+id/tv_1"
10         android:layout_width="match_parent"
11         android:layout_height="wrap_content"
12         android:layout_marginTop="40dp"
13         android:background="#000000"
14         android:gravity="center_horizontal"
15         android:padding="10dp"
16         android:text="系统默认的显示"
17         android:textColor="#ffffff"
18         android:textSize="16sp" />
19 
20 
21     <TextView
22         android:id="@+id/tv_2"
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content"
25         android:layout_marginTop="40dp"
26         android:background="#000000"
27         android:gravity="center_horizontal"
28         android:padding="10dp"
29         android:text="自定义Toast的样式"
30         android:textColor="#ffffff"
31         android:textSize="16sp" />
32 
33     <TextView
34         android:id="@+id/tv_3"
35         android:layout_width="match_parent"
36         android:layout_height="wrap_content"
37         android:layout_marginTop="40dp"
38         android:background="#000000"
39         android:gravity="center_horizontal"
40         android:padding="10dp"
41         android:text="自定义Toast的显示时间"
42         android:textColor="#ffffff"
43         android:textSize="16sp" />
44 
45 </LinearLayout>
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:id="@+id/ll_Toast"
 4     android:layout_width="wrap_content"
 5     android:layout_height="wrap_content"
 6     android:background="#ffffffff"
 7     android:orientation="vertical" >
 8 
 9     <LinearLayout
10         android:id="@+id/ll_ToastContent"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:layout_marginBottom="1dp"
14         android:layout_marginLeft="1dp"
15         android:layout_marginRight="1dp"
16         android:background="#44000000"
17         android:orientation="vertical"
18         android:padding="15dip" >
19 
20         <ImageView
21             android:id="@+id/iv_ImageToast"
22             android:layout_width="wrap_content"
23             android:layout_height="wrap_content"
24             android:layout_gravity="center" />
25 
26         <TextView
27             android:id="@+id/tv_TextToast"
28             android:layout_width="wrap_content"
29             android:layout_height="wrap_content"
30             android:gravity="center"
31             android:paddingLeft="10dp"
32             android:paddingRight="10dp"
33             android:textColor="#ff000000" />
34     </LinearLayout>
35 
36 </LinearLayout>

 

以上代码都很简单,应该不需要什么注释。

 

posted @ 2016-09-19 15:04  总是说  阅读(8772)  评论(0编辑  收藏  举报