zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    YOLO2 (2) 测试自己的数据
    Ubuntu 14.04服务器配置 (1) 安装和配置
    window10+linux双系统安装
    机械纪元 尼奥
    如何标数据
    usb-cam (3)摄像机标定文件-ORB-SLAM标定文件
    ORB-SLAM2(3) ROS下实时跑ORB_SLAM2
    usb-cam(1)安装
    usb-cam (2)摄像机标定
    Linux下的压缩zip,解压缩unzip命令详解及实例
  • 原文地址:https://www.cnblogs.com/HEWU10/p/4343512.html
Copyright © 2011-2022 走看看