Android的消息提示:Toast与Notification的使用

Toast:向用户提供比较快速的即时消息,当Toast被显示时,虽然其悬浮应用程序的最上方,但是Toast从不获得焦点。一般用于提示用户某项设置成功。

Toast对象的创建通过Toast类的静态方法makeText来实现,该方法有两个重载实现,主要的不同是一个接收字符串,一个接收字符串的资源标识符作为参数

Toast对象创建好后,调用其show方法即可将消息提示显示到屏幕上。

一般来讲,Toast只显示比较简短的文本信息,但Toast也可以显示图片。


Toast实例:

1.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="fill_parent"
     android:layout_height="fill_parent"
     >												<!-- 声明一个线性布局 -->
 	<Button  
 		android:id="@+id/btn"
 	    android:layout_width="fill_parent" 
 	    android:layout_height="wrap_content" 
 	    android:text="Click"
 	    />												<!-- 声明一个Button控件 -->
 </LinearLayout>

2.Activity

package wyf.jc;								//声明包语句
 import android.app.Activity;						//引入相关类
 import android.os.Bundle;				//引入相关类
 import android.view.Gravity;
 import android.view.View;						//引入相关类
 import android.widget.Button;				//引入相关类
 import android.widget.ImageView;				//引入相关类
 import android.widget.LinearLayout;				//引入相关类
 import android.widget.Toast;				//引入相关类
 public class Sample_6_9 extends Activity {
     @Override
     public void onCreate(Bundle savedInstanceState) {	//重写onCreate方法
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);					//设置当前屏幕
         Button btn = (Button)findViewById(R.id.btn);
         btn.setOnClickListener(new View.OnClickListener() {	//为按钮添加监听器
 			@Override
 			public void onClick(View v) {
 				ImageView iv = new ImageView(Sample_6_9.this);	//创建ImageView
 				iv.setImageResource(R.drawable.header2);		//设置ImageView的显示内容
 				LinearLayout ll = new LinearLayout(Sample_6_9.this);	//创建一个线性布局
 				Toast toast = Toast.makeText(Sample_6_9.this, "这是一个带图片的Toast", Toast.LENGTH_LONG);
 				toast.setGravity(Gravity.CENTER, 0, 0);
 				View toastView = toast.getView();			//获得Toast的View
 				ll.setOrientation(LinearLayout.HORIZONTAL);		//设置线性布局的排列方式
 				ll.addView(iv);							//将ImageView添加到线性布局
 				ll.addView(toastView);					//将Toast的View添加到线性布局
 				toast.setView(ll);
 				toast.show();							//显示Toast
 			}
 		});
     }
 }


Notification是另外一种消息机制,Notification位于手机的状态栏,状态栏位于手机的最上层,通常显示电量,信号强度等信息。在Android的手机中用手指按下状态栏并往下拉可以打开状态栏查看系统的提示信息

Notification实例:

strings.xml

<?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string name="hello">Hello World, Sample_6_10!</string>
     <string name="app_name">Sample_6_10</string>
     <string name="btn">点击添加Notification</string>
     <string name="tv">我是通过Notification启动的!</string>
     <string name="notification">点击查看</string>
 </resources>

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="fill_parent"
     android:layout_height="fill_parent"
     >							<!-- 声明一个线性布局 -->
 	<Button  	
 		android:id="@+id/btn"
 	    android:layout_width="fill_parent" 
 	    android:layout_height="wrap_content" 
 	    android:text="@string/btn"	
 	    />						<!-- 声明一个Button对象 -->
 </LinearLayout>
 
notified.xml

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content">					<!-- 声明线性布局 -->
 	<EditText
 		android:text="@string/tv"
 		android:layout_width="fill_parent"
 		android:layout_height="wrap_content"
 		android:editable="false"
 		android:cursorVisible="false"
 		android:layout_gravity="center_horizontal"
 		/>								<!-- 声明EditText控件 -->
 </LinearLayout>

Activity

package wyf.jc;					//声明包语句
 import android.app.Activity;		//引入相关类
 import android.app.Notification;		//引入相关类
 import android.app.NotificationManager;	//引入相关类
 import android.app.PendingIntent;		//引入相关类
 import android.content.Intent;			//引入相关类
 import android.os.Bundle;				//引入相关类
 import android.view.View;				//引入相关类
 import android.widget.Button;			//引入相关类
 public class Sample_6_10 extends Activity {
     @Override
     public void onCreate(Bundle savedInstanceState) {//重写onCreate方法
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);				//设置当前屏幕
         Button btn = (Button)findViewById(R.id.btn);	//获取Button对象
         btn.setOnClickListener(new View.OnClickListener() {		//为按钮设置监听器
 			@Override
 			public void onClick(View v) {						//重写onClick方法
 				Intent i = new Intent(Sample_6_10.this, NotifiedActivity.class);
 				PendingIntent pi = PendingIntent.getActivity(Sample_6_10.this, 0, i, 0);
 				Notification myNotification = new Notification();	//创建一个Notification对象
 				myNotification.icon=R.drawable.header;				//Notification的图标
 				myNotification.tickerText=getResources().getString(R.string.notification);			//
 				myNotification.defaults=Notification.DEFAULT_SOUND;
 				myNotification.setLatestEventInfo(Sample_6_10.this, "示例", "点击查看", pi);
 				NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
 				notificationManager.notify(0, myNotification);		//发送Notification
 			}
 		});
     }
 }


package wyf.jc;				//声明包语句
 import android.app.Activity;	//引入相关类
 import android.os.Bundle;		//引入相关类
 public class NotifiedActivity extends Activity {
     @Override
     public void onCreate(Bundle savedInstanceState) {	//重写onCreate方法
         super.onCreate(savedInstanceState);
         setContentView(R.layout.notified);			//设置当前屏幕
     }
 }





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值