Android学习之Toast

来源:互联网 发布:中网数据创始人孙远根 编辑:程序博客网 时间:2024/04/26 13:13

Toast用于向用户显示一些帮助/提示。平时我们常用的全文字和图文混排两种风格,其实现将在下面介绍:

1 主Acitivty的布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <Button        android:id="@+id/btn_1"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="纯文字的Toast"        android:textSize="20sp" />        <Button        android:id="@+id/btn_2"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="图文混排的Toast"        android:textSize="20sp" /></LinearLayout>

2 主Activity的实现

package com.example.toast;import android.os.Bundle;import android.app.Activity;import android.content.Context;import android.view.Gravity;import android.view.Menu;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {private Button btn_1 = null;private Button btn_2 = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);inital();setBtnOnClickListener();}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.activity_main, menu);return true;}private void inital() {btn_1 = (Button) findViewById(R.id.btn_1);btn_2 = (Button) findViewById(R.id.btn_2);}private void setBtnOnClickListener() {btn_1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubshowToast(1, MainActivity.this, "今天天气挺不错的~", Toast.LENGTH_SHORT);}});btn_2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubshowToast(2, MainActivity.this, "考完试了,心情不错 啊!!!!!", Toast.LENGTH_SHORT);}});}    /** * 显示toast * @param style 设置显示风格,1表示默认 2表示自定义 * @param context  * @param text 设置需要显示的文字 * @param duration 设置需要显示的时长 */private void showToast(int style, Context context, String text, int duration) {switch (style) {case 1:Toast.makeText(context, text, duration).show();break;case 2://通过反射获取自定义的ViewView view = getLayoutInflater().inflate(R.layout.toast_item, null);TextView tv_toast = (TextView)view.findViewById(R.id.tv_toast);//设置自定义view中显示的文字tv_toast.setText(text);Toast toast = new Toast(context);//设置显示时长toast.setDuration(duration);//加载需要显示的自定义viewtoast.setView(view);//设置toast显示的位置toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);//显示toasttoast.show();break;default:break;}}}

其中图文混排效果的布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center_vertical"    android:background="#C0C0C0"    android:orientation="horizontal" >        <ImageView        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:src="@drawable/ic_launcher"        />    <TextView        android:id="@+id/tv_toast"         android:layout_height="wrap_content"        android:layout_width="200dip"        android:layout_marginLeft="20dip"        android:text="考完试了,心情不错 啊!!!!!"        android:textSize="18sp"        /></LinearLayout>

3 最终效果如下