android 开发-Toast控件的实现

  • 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 }
时间: 2024-08-10 14:51:50

android 开发-Toast控件的实现的相关文章

CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建

Android开发使用控件入门--环境搭建 2014-12-24 09:57     14人阅读     评论(0)     收藏         编辑     删除 CAD控件.CAD三维控件,手机控件 Android开发控件 软件名称(,梦,,想.CAD  ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android SDK 5 1.4. 给Eclipse 安装ADT插件  8 1.5. 运行Eclipse设置Android ADT

Android开发_ImageButton控件

Android中的控件中的ImageButton有着一般Button控件所具有的一般特征,但有一个Image的脸 因此可以使用 setImageResource(resId); 来设定其图片资源. 经过测定,Button类组件的获取焦点,即setFocusChange()方法基本没有用,因为一般的Button都会响应点击时间,点击事件会覆盖掉获取焦点事件.

Android开发CheckBox控件,全选,反选,取消全选

在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_paren

Android开发ImageView控件缩放图片

首先还是最基础的ImageView控件如何显示图片: <ImageView                Android:id="@+id/imgView"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                android:src="@drawable

Android开发 设置控件边角半圆

1 <?xml version="1.0" encoding="utf-8"?> 2 <shape xmlns:android="http://schemas.android.com/apk/res/android" > 3 <corners android:radius="30dip" /> 4 <solid android:color="#ff3290f0" /

android 开发-设置控件/view的水平方向翻转

设置控件沿着水平方向翻转(即Y轴180°) 看效果: 代码: <pl.droidsonroids.gif.GifImageView android:id="@+id/gv_image1" android:layout_width="0dp" android:layout_weight="1" android:layout_height="150dp" android:scaleType="fitXY"

Android开发之控件-Android-PullToRefresh

下拉刷新,使用的是Android-PullToRefresh,Github--https://github.com/chrisbanes/Android-PullToRefresh PullToRefresh的简单使用办法 1.通过GitHub把相关的lib下载下来,导入到ADT 下载下来是这些文件 然后导入到ADT,导入以后是4个工程 LauncherActivityPullToRefreshListFragmentPullToRefreshViewPagerlibrary 假如导入以后,工程

【Android开发】控件外边框自定义

1.在drawable里面新建自定义的资源文件shape <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp"

从零开始学android开发-获取控件

mBtnNews = (Button)findViewById(R.id.btn_news);//获取控件