android开发步步为营之32:玩转Toast

来源:互联网 发布:阿里云服务器无法联网 编辑:程序博客网 时间:2024/05/16 11:12

Toast英文名为土司,在Android里面这个类是用来弹出提示信息的,我想sdk作者是认为提示信息片长得就像一块土司吧。这个理论就不多说什么了,开始我们的实践。
第一步、设计页面
主页面toastview.xml
<?xml version="1.0"encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button android:layout_height="wrap_content"android:text="默认" android:id="@+id/btnSimpleToast"android:layout_width="fill_parent" android:layout_alignParentTop="true"android:layout_alignParentLeft="true"></Button>
    <Button android:layout_height="wrap_content"android:text="自定义显示位置" android:id="@+id/btnSimpleToastWithCustomPosition"android:layout_width="fill_parent" android:layout_below="@+id/btnSimpleToast" android:layout_alignParentLeft="true"android:layout_marginTop="21dp"></Button>
    <Button android:layout_height="wrap_content"android:text="带图片" android:id="@+id/btnSimpleToastWithImage"android:layout_width="fill_parent" android:layout_below="@+id/btnSimpleToastWithCustomPosition"android:layout_alignParentLeft="true"android:layout_marginTop="20dp"></Button>
    <Button android:layout_height="wrap_content"android:text="其他线程" android:id="@+id/btnRunToastFromOtherThread"android:layout_width="fill_parent" android:layout_below="@+id/btnCustomToast" android:layout_alignParentLeft="true"android:layout_marginTop="26dp"></Button>
    <Button android:layout_height="wrap_content"android:text="完全自定义" android:id="@+id/btnCustomToast"android:layout_width="fill_parent" android:layout_below="@+id/btnSimpleToastWithImage" android:layout_alignParentLeft="true"android:layout_marginTop="19dp"></Button>
</RelativeLayout>
 
自定义toast页面customtoast.xml
<?xml version="1.0"encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_height="wrap_content" android:layout_width="wrap_content"
 android:background="#ffffffff" android:orientation="vertical"
 android:id="@+id/llToast" >
 
    <TextView android:layout_height="wrap_content"
       android:layout_margin="1dip" android:textColor="#ffffffff"
       android:layout_width="fill_parent" android:gravity="center"
       android:background="#bb000000" android:id="@+id/tvTitleToast"/>
    <LinearLayout android:layout_height="wrap_content"
       android:orientation="vertical" android:id="@+id/llToastContent"
       android:layout_marginLeft="1dip" android:layout_marginRight="1dip"
       android:layout_marginBottom="1dip" android:layout_width="wrap_content"
       android:padding="15dip" android:background="#44000000">
       <ImageView android:layout_height="wrap_content"
           android:layout_gravity="center" android:layout_width="wrap_content"
           android:id="@+id/imgToast" />
       <TextView android:layout_height="wrap_content"
           android:paddingRight="10dip" android:paddingLeft="10dip"
           android:layout_width="wrap_content" android:gravity="center"
           android:textColor="#ff000000" android:id="@+id/tvTextToast"/>
     </LinearLayout>
</LinearLayout>
 
 
第二步、设计Acttivity ToastActivity.java
 
/**
 *
 */
package com.figo.helloworld;
 
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
 
/**
 * @author Administrator
 *
 */
public class ToastActivity extends Activity implementsOnClickListener  {
   Handler handler = new Handler();//帮助主线程和子线程进行通信
 
 
 
   @Override
   public void onCreate(BundlesavedInstanceState) {
       super.onCreate(savedInstanceState);
       //找到页面
       setContentView(R.layout.toastview);
       //添加按钮事件
       findViewById(R.id.btnSimpleToast).setOnClickListener(this);
       findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
              this);
       findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
       findViewById(R.id.btnCustomToast).setOnClickListener(this);
       findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
 
   }
 
   public void showToast() {
       handler.post(new Runnable() {
 
          @Override
          public void run() {
              Toast.makeText(getApplicationContext(),"我来自其他线程!",
                     Toast.LENGTH_SHORT).show();
 
          }
       });
   }
 
   @Override
   public void onClick(View v) {
       Toast toast = null;
       switch (v.getId()) {
       case R.id.btnSimpleToast://简单toast
          Toast.makeText(getApplicationContext(),"默认Toast样式",
                 Toast.LENGTH_SHORT).show();
          break;
       caseR.id.btnSimpleToastWithCustomPosition://自定义位置的toast
          toast =Toast.makeText(getApplicationContext(), "自定义位置Toast",
                 Toast.LENGTH_LONG);
          toast.setGravity(Gravity.CENTER,0, 0);
          toast.show();
          break;
       caseR.id.btnSimpleToastWithImage://带图片的toast
          toast =Toast.makeText(getApplicationContext(), "带图片的Toast",
                 Toast.LENGTH_LONG);
          toast.setGravity(Gravity.CENTER,0, 0);
          LinearLayout toastView =(LinearLayout) toast.getView();
          ImageView imageCodeProject= new ImageView(getApplicationContext());
          imageCodeProject.setImageResource(R.drawable.icon);
          toastView.addView(imageCodeProject,0);
          toast.show();
          break;
       case R.id.btnCustomToast://自定义toast
          LayoutInflaterinflater = getLayoutInflater();
          View layout =inflater.inflate(R.layout.customtoast,
                 (ViewGroup)findViewById(R.id.llToast));
          ImageView image =(ImageView) layout
                 .findViewById(R.id.imgToast);
          image.setImageResource(R.drawable.icon);
          TextView title = (TextView)layout.findViewById(R.id.tvTitleToast);
          title.setText("Attention");
          TextView text = (TextView)layout.findViewById(R.id.tvTextToast);
          text.setText("完全自定义Toast");
          toast = newToast(getApplicationContext());
          toast.setGravity(Gravity.RIGHT| Gravity.TOP, 100, 60);
          toast.setDuration(Toast.LENGTH_LONG);
          toast.setView(layout);
          toast.show();
          break;
       caseR.id.btnRunToastFromOtherThread://其他线程执行完后在主线程显示toast
          new Thread(new Runnable() {
              public void run() {
                 showToast();
              }
          }).start();
          break;
 
       }
 
   }
 
}
 
第三步、AndroidManifest.xml注册Activity
<activity android:name=".ToastActivity" android:label="@string/app_name">
           <intent-filter>
              <action android:name="android.intent.action.MAIN"/>
              <category android:name="android.intent.category.LAUNCHER"/>
           </intent-filter>
   </activity>
第四步、运行效果
 

0 0
原创粉丝点击