第36章、信息提示框Toast(从零开始学Android)

  Toast用于向用户显示一些帮助或者提示,对于我们来说已经不陌生了,经常用到。

  下面我们一起再深入了解一下Toast,你会惊奇发现Toast原来还能这样做!

 

一、设计界面

  1、打开“res/layout/activity_main.xml”文件。

  从工具栏向activity拖出5个按钮Button。

  

  2、打开activity_main.xml文件。

  代码如下:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
 	android:orientation="vertical" 
 	android:layout_width="match_parent"
 	android:layout_height="match_parent"
 	android:padding="5dip" 
 	android:gravity="center">
 	
 	<Button android:layout_height="wrap_content"
  			android:layout_width="match_parent"
  			android:id="@+id/original"
  			android:text="默认样式" />
 
 	<Button android:layout_height="wrap_content"
  			android:layout_width="match_parent" 
  			android:id="@+id/byphoto"
  			android:text="带图片样式"/>
 
 	<Button android:layout_height="wrap_content"
  			android:layout_width="match_parent"
  			android:text="自定义显示位置样式"
  			android:id="@+id/customposition"/>
 
 	<Button android:layout_height="wrap_content"
  			android:layout_width="match_parent"
  			android:text="完全自定义样式"
  			android:id="@+id/alldiy"/>
 
 	<Button android:layout_height="wrap_content"
  			android:layout_width="fill_parent" 
  			android:text="来自其他线程样式"
  			android:id="@+id/bythread"/>
 
</LinearLayout>


  3、添加custom.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/customtoast" >
 
 	<TextView
  		android:layout_height="wrap_content"
  		android:layout_margin="1dip"
  		android:textColor="#ffffffff"
  		android:layout_width="match_parent"
  		android:gravity="center"
  		android:background="#16ccdd"
  		android:id="@+id/titletoast" />
 
 	<LinearLayout
  		android:layout_height="wrap_content"
  		android:orientation="vertical"
  		android:id="@+id/customtoastcontent"
  		android:layout_marginLeft="1dip"
  		android:layout_marginRight="1dip"
  		android:layout_marginBottom="1dip"
  		android:layout_width="wrap_content"
  		android:padding="15dip"
  		android:background="#ccffff" >

 		<ImageView
   			android:layout_height="wrap_content"
   			android:layout_gravity="center"
   			android:layout_width="wrap_content"
   			android:id="@+id/picture" />
  
  		<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/prompt" />
 	</LinearLayout>
</LinearLayout>

二、程序文件 

  打开“src/com.genwoxue.toast/MainActivity.java”文件。

  然后输入以下代码:  

package com.genwoxue.toast;

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.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
	
	 Handler handler = new Handler();
	 //声明按钮
	 private Button btnOriginal=null;
	 private Button btnByPhoto=null;
	 private Button btnCustomPosition=null;
	 private Button btnAllDiy=null;
	 private Button btnThread=null;
	 
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.activity_main);
		 //获得按钮
		 btnOriginal=(Button)super.findViewById(R.id.original);
		 btnByPhoto=(Button)super.findViewById(R.id.byphoto);
		 btnCustomPosition=(Button)super.findViewById(R.id.customposition);
		 btnAllDiy=(Button)super.findViewById(R.id.alldiy);
		 btnThread=(Button)super.findViewById(R.id.bythread);
		 //设置OnClick监听事件
		 btnOriginal.setOnClickListener(this);
		 btnByPhoto.setOnClickListener(this);
		 btnCustomPosition.setOnClickListener(this);
		 btnAllDiy.setOnClickListener(this);
		 btnThread.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.original:                  //默认Toast样式处理
		 		Toast.makeText(getApplicationContext(), "默认Toast样式",Toast.LENGTH_SHORT).show();
		 		break;
		 		
		 	case R.id.byphoto:                  //带图片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.ic_launcher);
		 		toastView.addView(imageCodeProject, 0);
		 		toast.show();
		 		break;
		 		
		 	case R.id.customposition:           //自定义位置Toast样式处理
		 		toast = Toast.makeText(getApplicationContext(),"自定义位置Toast", Toast.LENGTH_LONG);
		 		toast.setGravity(Gravity.CENTER, 0, 0);
		 		toast.show();
		 		break;
	 
		 	case R.id.alldiy:                   //完全自定义位置Toast样式处理
		 		
		 		/* setContentView()一旦调用, layout就会立刻显示UI;而inflate只会把Layout形成一个以view类实现成的
		 		 * 对象。 有需要时再用setContentView(view)显示出来。一般在activity中通过setContentView()将界面显
		 		 * 示出来,但是 如果在非activity中如何对控件布局设置操作了,这就需要LayoutInflater动态加载。		 */
		 		LayoutInflater inflater = getLayoutInflater();		 		
		 		//inflate(int Resourece,ViewGroup root)作用:填充一个新的视图层次结构从指定的XML资源文件中
		 		View layout = inflater.inflate(R.layout.custom,(ViewGroup) findViewById(R.id.customtoast));		 		
		 		//加载图像
		 		ImageView image = (ImageView) layout.findViewById(R.id.picture);
		 		image.setImageResource(R.drawable.ic_launcher);		 		
		 		//设置标题
		 		TextView title = (TextView) layout.findViewById(R.id.titletoast);
		 		title.setText("完全自定义Toast");		 		
		 		//显示内容
		 		TextView text = (TextView) layout.findViewById(R.id.prompt);
		 		text.setText("显示提示信息……");
		 		toast = new Toast(getApplicationContext());
		 		toast.setGravity(Gravity.RIGHT | Gravity.TOP, 12, 40);
		 		toast.setDuration(Toast.LENGTH_LONG);
		 		toast.setView(layout);
		 		toast.show();
		 		break;
		 		
		 	case R.id.bythread:
		 		//实例化线程
		 		new Thread(new Runnable() {
		 			public void run() {
		 				showToast();
		 			}
		 		}).start();
		 		break;
		 }
	 
	   }
}

说明:  

  Toast是Android中用来显示显示信息的一种机制,和Dialog不一样的是,Toast是没有焦点的,而且Toast显示的时间有限,过一定的时间就会自动消失。

三、运行效果

    

    

  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蒋会全

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值