0,消息提示
1,Toast
2,Notification
-
0,消息提示
当程序有大量信息需向用户提示,可以考虑对话框
如果只是少量信息,可以考虑“更轻量级”的消息提示
-
1,消息提示
Toast是一个非常方便的提示消息框,
特点:①不会获得焦点 ②过一段时间会自动消失
使用步骤:
①调用Toast构造器或makeText(),创建一个Toast对象
②调用Toast的方法设置消息提示的对齐方式,边距和内容等
③调用Toast的show()
实例:
public class ToastActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(ToastActivity.this, "简单的Toast", Toast.LENGTH_SHORT).show(); } }); findViewById(R.id.button2).setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast toast = Toast.makeText(ToastActivity.this, "带图片的Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); View view = toast.getView(); ImageView imageView = new ImageView(ToastActivity.this); imageView.setImageResource(R.drawable.ic_launcher); LinearLayout layout = new LinearLayout(ToastActivity.this); layout.addView(imageView); layout.addView(view); toast.setView(layout); toast.show(); } }); } }
-
2,Notify
手机状态栏的通知
使用步骤:
①getSystemService(NOTIFICATION_SERVICE)获得NotificationManager服务
②通过构造器创建一个Notification对象
③为Notification设置各种参数
④通过NotificationManager发送Notification
public class NotificationActivity extends Activity { static final int My_ID = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.button1).setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent intent = new Intent(NotificationActivity.this, OtherActivity.class); PendingIntent pi = PendingIntent.getActivity(NotificationActivity.this, 0, intent, 0); Notification notification = new Notification(); notification.icon = R.drawable.ic_launcher; notification.tickerText = "启动其他Activity的链接"; notification.when = System.currentTimeMillis(); notification.defaults = Notification.DEFAULT_ALL; notification.setLatestEventInfo(NotificationActivity.this, "通知", "点击进入OtherActivity", pi); NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.notify(My_ID, notification); } }); findViewById(R.id.button2).setOnClickListener(new OnClickListener() { public void onClick(View v) { NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); nm.cancel(My_ID); } }); } }
解析:
①PendingIntent对象封装了一个Intent,所以单击Notification会调整页面