文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>android实战用户接口第一篇——notification

android实战用户接口第一篇——notification

时间:2011-03-27  来源:小小石头

notification之toast:

1.创建一个toast:直接在Activity类里调用

Toast.makeText(this.getApplicationContext(), "hello wrold", Toast.LENGTH_LONG).show();

就可以了,这里有一点要说明下,函数的第一个参数要求一个Context对象,你可以直接用this,但这样容易造成内存泄漏,可以用this.getApplicationContext()来代替。

2.创建一个自定义的toast:首先你要根据需求为你的toast设计一个layout,并放上合适的控件

<?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"
    android:padding="10dp"
    android:background="@null"
    >
    <ImageView 
        android:layout_height="fill_parent" 
        android:id="@+id/image" 
        android:layout_width="wrap_content"
        android:layout_marginRight="10dp"
        />
    <TextView 
        android:id="@+id/text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:textColor="#fff"
        />
</LinearLayout>

前台代码是

Toast toast = new Toast(this.getApplicationContext());
                                
LayoutInflater inflater = this.getLayoutInflater();
View toastLayout = inflater.inflate(R.layout.customtoast, null);
                                
TextView text = (TextView) toastLayout.findViewById(R.id.text);
text.setText( "hello I am custom toast!");
                                
ImageView image = (ImageView) toastLayout.findViewById(R.id.image);
image.setBackgroundResource(R.drawable.icon);
                                
toast.setView(toastLayout);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();

这样便可以得到我们梦寐以求的toast了。

notification之Dilog:

用setXXXButton()创建一个带button的dilog:

//实例化一个builder对象,用来创建dialog
AlertDialog.Builder builder = new AlertDialog.Builder(this);

//添加两个button给biulder
builder.setMessage("Are you sure you want to exit?")
         .setCancelable(false)
         .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                         finish();
                  }
          }) 
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
                 public void onClick(DialogInterface dialog, int id) {
                          dialog.cancel();
                                           }
          });
//生了,生了,biulder生了
AlertDialog alert = builder.create();
//显示dialogs
alert.show();
用 setItems()创建一个带list的dialog
final CharSequence[] items = {"Red", "Green", "Blue"};

AlertDialog.Builder builder
= new AlertDialog.Builder(this);
builder.setTitle(
"Pick a color");
builder.setItems(items,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert
= builder.create();
alert.show();
android还提供了progressbar等一些常用的dialog,不建议搞非主流,就是不要既有button又想加list顺便还想带个progressbar,否则会在设置上有冲突,引起混乱,如果实在是要用很多种控件的话,可以自定义dialog,首先是设计一个layout文件(跟上面的toast一样,这里代码就免了^_^),然后在前台添加代码
Dialog dialog = new Dialog(this);//贼里官方给的是getApplicationContext(),但事实是会出错!

dialog.setContentView(R.layout.customtoast);
dialog.setTitle(
"Custom Dialog");

TextView text
= (TextView);
dialog.findViewById(R.id.text);
text.setText(
"Hello, this is a custom dialog!");
ImageView image
= (ImageView);
dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);

dialog.show();
如果想让dialog退出,调用dismiss()就可以了。

notification之Status Bar Notifications:

一般用在service里,首先要创建一个服务,然后在服务里加入代码

NotificationManager nm = (NotificationManager) this.getSystemService(this.NOTIFICATION_SERVICE);
                
int icon = R.drawable.icon;
CharSequence tickerText = "this is in service"+i++;
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);
                
Context context = getApplicationContext();
CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!"+i++;
Intent notificationIntent = new Intent(this, notificationDemo.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
                
nm.notify(1, notification);
未完待续...
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载