skywang12345

导航

 

Android控件之Toast

1 Toast说明

Toast的作用是给出一个简短的说明信息。例如,当你要显示一段提示语给用户时(比如“设置已保存”),你可以使用Toast。Toast永远都不会获取节焦点


2 Toast示例

创建一个activity,包含3个按钮。
点击按钮1,创建Toast(即toast01)。
点击按钮2,若toast01仍然显示,则关闭toast01;否则什么都不做。
点击按钮3,创建Toast(即toast02)。
按钮1和按钮3的Toast的创建方法稍微有点差异。


应用层代码

package com.skywang.control;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Toast;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.util.Log;

public class ToastTest extends Activity implements View.OnClickListener{
    private static final String TAG = "SKYWANG";

    private Button mBtnShow;
    private Button mBtnVanish;
    private Button mBtnStatic;
    private Toast mToast;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.toast_test);
        
        // 设置show按钮
        mBtnShow = (Button)findViewById(R.id.btn_show);
        // 设置show按钮的监听函数
        mBtnShow.setOnClickListener(this);

        // 设置vanish按钮
        mBtnVanish = (Button)findViewById(R.id.btn_vanish);
        // 设置vanish按钮的监听函数
        mBtnVanish.setOnClickListener(this);

        // 设置static按钮
        mBtnStatic = (Button)findViewById(R.id.btn_static);
        // 设置static按钮的监听函数
        mBtnStatic.setOnClickListener(this);
        
        // 设置Toast
        mToast = Toast.makeText(getApplicationContext(), R.string.text_dyn, Toast.LENGTH_LONG);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_show: {
                // 显示Toast
                if (mToast != null)
                    mToast.show();
                break;
            }
            case R.id.btn_vanish: {
                // 取消Toast
                if (mToast != null)
                    mToast.cancel();
                break;
            }
            case R.id.btn_static: {
                Toast.makeText(getApplicationContext(), getString(R.string.text_sta), Toast.LENGTH_SHORT).show();
                break;
            }
        }
    }
    
}

 

layout文件

<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" >

    <Button
        android:id="@+id/btn_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_show" />

    <Button
        android:id="@+id/btn_vanish"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_vanish" />
    
    <Button
        android:id="@+id/btn_static"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_static" />
    
</LinearLayout>

 

manifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.skywang.control"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.skywang.control.ToastTest"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

点击下载源代码

 

运行效果图:

 

posted on 2013-06-15 13:12  如果天空不死  阅读(1067)  评论(0编辑  收藏  举报