Android_文档学习_UI_Notifying the User

Notifying the User

本文测试代码csdn下载频道:http://download.csdn.net/source/2907960

通知用户的方式包括3种.一种是Toast一种是Status Bar(状态栏),还有一种是前面已经提到过的Dialog(对话框).具体的我就不介绍了,因为网上也介绍的比较清楚.我就把怎么用的,然后使用过程中,需要注意到的一些写一下.这样读者读起来就比较轻松了.

1.Creating Toast Notifications

首先,要具体化一个Toast对象用makeText()方法.这个方法需要3个参数.包括应用程序的Context,text message,duration.然后才会返回一个具体化的Toast对象.你可以用show()方法,显示出Toast来.代码如下:

Context context = getApplicationContext(); CharSequence text = "Hello toast!"; int duration = Toast.LENGTH_SHORT; Toast toast = Toast.makeText(context, text, duration); toast.show();Toast效果图

你也可以将代码简化,用下面的代码:

Toast.makeText(context, text, duration).show();

用setGravity(int, int, int)方法,接收3个参数.改变Toast显示的位置.参数用户可以自己调试然后看效果.

代码如下:toast.setGravity(Gravity.TOP|Gravity.LEFT, 0, 0);

Creating a Custom Toast View

创建自定义的Toast可以现在layout/目录下,建一个xml文件(如:toast_layout.xml):用如下代码自定义Toast

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toast_layout_root" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:background="#DAAA" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_marginRight="10dp" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="fill_parent" android:textColor="#FFF" /> </LinearLayout>

然后在java文件中写入如下代码:

LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root)); ImageView image = (ImageView) layout.findViewById(R.id.image); image.setImageResource(R.drawable.android); TextView text = (TextView) layout.findViewById(R.id.text); text.setText("Hello! This is a custom toast!"); Toast toast = new Toast(getApplicationContext()); toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0); toast.setDuration(Toast.LENGTH_LONG); toast.setView(layout); toast.show();

步骤1,先用 getLayoutInflater() (或 getSystemService() )检索LayoutInflater,然后用inflate(int, ViewGroup)方法从xml文件中inflate布局.

步骤2,捕捉定义了的View对象(例子中是ImageView and TextView elements.)

步骤3,创建一个Toast,然后设置toast的一些属性.再调用setView(View)设置布局.最后调用show()方法.

注意:不要使用public构造方法,除非你将要用setView(View)方法 定义这个布局.如果你没有自定义的toast要用的话,你必须用 makeText(Context, int, int)创建一个Toast.

2.Creating Status Bar Notifications

1.先定义一个静态常量

private static final int HELLO_ID = 1;

然后在onCreate或者其他方法中写如下代码:2F

//1获得一个NotificationManager的引用 String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); //2实例化通知 int icon = R.drawable.icon; CharSequence tickerText = "Hello"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); //3定义一个通知的扩展信息和intent Context context = getApplicationContext(); CharSequence contentTitle = "My notification"; CharSequence contentText = "Hello World!"; Intent notificationIntent = new Intent(this, MyClass.class);//两个参数分别为,当前的Activity和将要跳转的Activity PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); //4将当前的通知传给NotificationManager mNotificationManager.notify(HELLO_ID, n

本文测试代码csdn下载频道:http://download.csdn.net/source/2907960

新手初学,希望写的不好的地方.或者我哪里理解错误的地方.同学们能帮我指出来,让我明白.

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值