1. 用Toast显示提示信息框
一般,Toast信息框,可以自动关闭。下面先介绍自动关闭的Toast设置。主要有两种方法
Toast textToast = Toast.makeText( this, " 真好的天气 ", Toast.LENGTH_LONG);
textToast.show();
上面的代码使用Toast类的静态方法创建了一个Toast对象,Toast.LENGTH_LONG 表示Toast显示时间,还有一个值为Toast.LENGTH_SHORT
创建Toast对象时要注意,在创建只显示文本的Toast对象时建议使用makeText方法,而不要直接new一个Toast对象。虽然Toast对象有setText方法,但是不能再使用new关键字创建一个新的Toast对象之后,再用setText使用使用,也就是说,下面的代码是错误的。
Toast toast = new Toast(this);
toast.setText("今天的天气真好");
toast.show();
第二种方法
View view = getLayoutInflater().inflate(R.layout.toast, null );
TextView textview = (TextView) view.findViewById(R.id.textview);
textView.setText("Nice weather");
Toast toast = new Toast(this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(view);
toast.show();
(使用这种方法,可以在Toast中加入图片,自定义的一些布局之类的,而不只是单纯的文字,第一种方法的局限性在于只能是文字)
2.创建不自动关闭的Toast
Java的private成员不是不可访问,需要更高级的Java反射技术访问这些成员。
Toast类中有一个内嵌类TN, TN 类中有两个方法:show和hide。实际上,这两个方法分别用来显示和关闭Toast信息框。但TN类被声明为private,因此通过常规方法无法访问该类。
下面的代码通过Java反射技术获得了Toast.mTN变量的值,并在此通过反射技术调用show和hide方法。
Main.Java
package mobile.android.ch07.control.toast;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class Main extends Activity
{
private Object obj;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
//显示Toast信息框
public void onClick_ShowToast(View v)
{
Toast toast = Toast.makeText(this, "不会自动关闭的Toast信息框", Toast.LENGTH_SHORT);
//设置Toast信息框上屏幕顶端水平方向显示
toast.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
try
{
//获取mTN变量对应的Field对象
Field field = toast.getClass().getDeclaredField("mTN");
//允许访问mTN对象
field.setAccessible(true);
//获取mTN对象
obj = field.get(toast);
//再次通过反射机制,获得mTNT对象中的show方法
Method method = obj.getClass().getDeclaredMethod("show", null);
//调用show方法显示Toast信息框
method.invoke(obj, null);
}
catch (Exception e)
{
}
}
public void onClick_CloseToast(View v)
{
try
{
调用hide方法关闭Toast信息框
Method method = obj.getClass().getDeclaredMethod("hide", null);
method.invoke(obj, null);
}
catch (Exception e)
{
// TODO: handle exception
}
}
}
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:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="显示Toast信息提示框"
android:onClick="onClick_ShowToast" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="关闭Toast信息提示框"
android:onClick="onClick_CloseToast" />
</LinearLayout>
3.Notification状态栏
Notification与Toast都可以起到通知的作用,但是Notification感觉更高大上,没有对话框,二,Notification需要用NotificationManager来管理,Toast只需要简单的创建Toast对象就可以了。
创建步骤:
//首先需要用到这样一句,获得notificationManager
private NotificationManager notificatioManager;
notificatioManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// 这句话可以放在Activity的onCreate()方法中。
//之后用下面的四个步骤去设置Notification
Notification notification = new Notification(R.drawable.time,"程阳来找你啦", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,0,getIntent(),0);
notification.defaults = Notification.DEFAULT_ALL;
notification.setLatestEventInfo(MainActivity.this, "天气预报", "青砖白蛙", contentIntent);
notificatioManager.notify(R.drawable.time,notification);
可以专门写一个函数,来提高代码重用率
private void showNotification(String tickerText, String contentTitle,
String contentText, int id, int resId)
{
Notification notification = new Notification(resId,
tickerText, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
getIntent(), 0);
notification.setLatestEventInfo(this, contentTitle, contentText,
contentIntent);
notificationManager.notify(id, notification);
}
showNotification("今天非常高兴", "今天考试得了全年级第一",
"数学100分、语文99分、英语100分,yeah!", R.drawable.smile,
R.drawable.smile);
showNotification("这是为什么呢?", "这道题为什么会出错呢?", "谁有正确答案啊.",
R.drawable.why, R.drawable.why);
在显示Notification时,还可以设置显示通知时的默认发声、震动和Light效果,要实现这个功能,需要设置Notification的default属性
notification.defaults = Notification.DEFAULT_ALL;
notification.defaults = Notification.DEFAULT_SOUND;
notification.defaults = Notification.DEFAULT_VIBRATE;
notification.defaults = Notification.DEFAULT_LIGHT;
如果想要设置为vibrate,需要在AndroidManifest.xml文件中通过这样一句话,声明权限。
<uses-permission android:name="android.permission.VIBRATE"/>
刚才调试的时候,被一个Bug坑了。主要原因是在给一个按钮添加onClick函数的时候,忘记了正确的函数签名,具体为参数列表不正确。
android:onClick="btnShow"
private void btnShow(**View view**) //千万要记得这里的View view
{
//xxx
}
刚才又发现一个问题,Notification不会自动消失的…百度了一下,发现了应该多设置一句话:
public void sendNotification(View view)
{
Notification notification = new Notification(R.drawable.time,"程阳来找你啦", System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this,0,getIntent(),0);
notification.defaults = Notification.DEFAULT_ALL;
notification.setLatestEventInfo(MainActivity.this, "天气预报", "青砖白蛙", contentIntent);
notification.flags = Notification.FLAG_AUTO_CANCEL;
//********************这里,需要设置auto_cancel...******************
notificatioManager.notify(R.drawable.time,notification);
// new AlertDialog.Builder(MainActivity.this).setTitle(“xxx”).setMessage(“xxx”).create().show();
}