码迷,mamicode.com
首页 > 移动开发 > 详细

android 开发-Toast控件的实现

时间:2015-03-17 10:24:03      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:

  • Toast吐司:

  Toast内容简单,不做过多介绍,Toast支持自带简单吐司,自定义吐司。内容简单可见代码,详见API。A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

  •   代码示例

  activity_main.xml

    

技术分享
 1 <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context=".MainActivity" >
10 
11     <Button
12         android:id="@+id/button1"
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:layout_alignParentTop="true"
16         android:layout_centerHorizontal="true"
17         android:layout_marginTop="190dp"
18         android:text="吐司按钮" />
19 
20     <Button
21         android:id="@+id/button2"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:layout_alignLeft="@+id/button1"
25         android:layout_below="@+id/button1"
26         android:layout_marginTop="20dp"
27         android:text="自定义吐司" />
28 
29 </RelativeLayout>

 

  toast_layout.xml

    

技术分享
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/toast_layout_root"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:padding="8dp"
              android:background="#DAAA">
              
    <ImageView android:src="@drawable/droid"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_marginRight="8dp"
               />
    <TextView android:id="@+id/text"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:textColor="#FFF"
              />
</LinearLayout>

  Activity

 1 package com.example.android_toast;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Gravity;
 6 import android.view.Menu;
 7 import android.view.View;
 8 import android.view.View.OnClickListener;
 9 import android.view.ViewGroup;
10 import android.widget.Button;
11 import android.widget.TextView;
12 import android.widget.Toast;
13 /**
14  * @author xiaowu
15  * NOTE:Toast吐司,支持自带简单吐司,自定义吐司。内容简单可见代码,详见API
16  */
17 public class MainActivity extends Activity {
18     private Button button ;
19     private Button button2 ;
20     @Override
21     protected void onCreate(Bundle savedInstanceState) {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         button = (Button) this.findViewById(R.id.button1);
25         button2 = (Button)this.findViewById(R.id.button2);
26         
27         //为按钮添加点击监听事件
28         button.setOnClickListener(new View.OnClickListener() {
29             @Override
30             public void onClick(View v) {
31                 // TODO Auto-generated method stub
32                 //Toast.makeText(MainActivity.this, "吐司内容", 1).show();                
33                 //创建吐司对象
34                 Toast toast =Toast.makeText(MainActivity.this, "吐司内容", 0);
35                 //设置吐司在视图中显示的位置
36                 toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);
37                 toast.show();
38             }
39         });
40         
41         //为按钮添加点击监听事件
42         button2.setOnClickListener(new OnClickListener() {
43             @Override
44             public void onClick(View v) {
45                 // TODO Auto-generated method stub
46                 //通过xml配置文件,加载自定义吐司视图
47                 View view = getLayoutInflater().inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));
48                 //通过该视图查找视图中的对象textView
49                 TextView textView = (TextView) view.findViewById(R.id.text);
50                 textView.setText("吐司内容");
51 //                Toast toast = new Toast(MainActivity.this);
52                 Toast toast = new Toast(getApplicationContext());
53                 //设置吐司在视图中显示的位置
54                 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
55                 //设置吐司限时时长
56                 toast.setDuration(Toast.LENGTH_LONG);
57                 toast.setView(view);
58                 toast.show();
59             }
60         });
61         
62     }
63 
64     @Override
65     public boolean onCreateOptionsMenu(Menu menu) {
66         // Inflate the menu; this adds items to the action bar if it is present.
67         getMenuInflater().inflate(R.menu.main, menu);
68         return true;
69     }
70 
71 }

 

android 开发-Toast控件的实现

标签:

原文地址:http://www.cnblogs.com/HEWU10/p/4343512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!