带图片的Toast
效果图如下:
代码如下:
Button toa = (Button) findViewById(R.id.btn1); toa.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast t = Toast.makeText(Main.this, "带图片的toast提示", Toast.LENGTH_LONG); t.setGravity(Gravity.CENTER, 0, 0); View tv = t.getView(); LinearLayout l = new LinearLayout(Main.this); ImageView img = new ImageView(Main.this); img.setImageResource(R.drawable.z1); l.addView(img); l.addView(tv); t.setView(l); t.show(); } });
Notification 的使用:
效果:点击发送Notification,将会看到系统通知,点击,将会执行自定的操作;
代码如下:
public class Main extends Activity {
static final int id = 0x1123;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button send = (Button) findViewById(R.id.btn1);
Button cancel = (Button) findViewById(R.id.btn);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Main.this, Alert.class);
PendingIntent pi = PendingIntent
.getActivity(Main.this, 0, i, 0);
Notification n = new Notification();
n.icon = R.drawable.z1;
n.tickerText = "启动其它通知";
n.when = System.currentTimeMillis();
n.defaults = Notification.DEFAULT_ALL;
n.defaults = Notification.DEFAULT_SOUND;// 设置使用默认声音
n.setLatestEventInfo(Main.this, "普通通知", "点击查看", pi);
// 获取系统服务
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// 发送通知
nm.notify(id, n);
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(id);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private void show(String s) {
Toast.makeText(Main.this, s, Toast.LENGTH_SHORT).show();
}
}