DatePickerDialog、TimePickerDialog、PopupWindow、Notification
来源:互联网 发布:ico透明图标制作软件 编辑:程序博客网 时间:2024/04/30 12:08
- Dialog
- DatePickerDialog
- 运行图
- TimePickerDialog
- 结果图
- PopupWindow
- 布局文件
- 运行图
- Notification主代码
- 最新版本
- 运行图
- 自定义的Notification
- DatePickerDialog
Dialog
DatePickerDialog
mCalendar=Calendar.getInstance(); DatePickerDialog datedialog=new DatePickerDialog(MainActivity.this, new OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { mCalendar.set(year, monthOfYear,dayOfMonth); SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日"); Toast.makeText(getApplicationContext(), "这个事"+format.format(mCalendar.getTime()), Toast.LENGTH_SHORT).show(); } }, mCalendar.get(Calendar.YEAR), mCalendar.get(Calendar.MONTH), mCalendar.get(Calendar.DAY_OF_MONTH)); datedialog.show();
运行图
TimePickerDialog
private Calendar mCalendar;mCalendar=Calendar.getInstance(); TimePickerDialog timedialog=new TimePickerDialog(MainActivity.this,new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { mCalendar.set(Calendar.HOUR, hourOfDay); mCalendar.set(Calendar.MINUTE, minute); SimpleDateFormat formattime=new SimpleDateFormat("yyyy年MM月dd日HH----mm"); Toast.makeText(getApplicationContext(), formattime.format(mCalendar.getTime()),Toast.LENGTH_SHORT).show(); } },mCalendar.get(Calendar.HOUR),mCalendar.get(Calendar.MINUTE),true); timedialog.show();
结果图
PopupWindow
private PopupWindow popwindow;popwindow=new PopupWindow(MainActivity.this); View popView=getLayoutInflater().inflate(R.layout.my_pop, null); popwindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); popwindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); popwindow.setContentView(popView); popwindow.setFocusable(false); popwindow.setOutsideTouchable(true); popwindow.showAsDropDown(button10);//添加此代码是按返回键可以取消。@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode==KeyEvent.KEYCODE_BACK){ if(popwindow!=null&&popwindow.isShowing()){ popwindow.dismiss(); //返回true是避免继续向下执行 return true; } } return super.onKeyDown(keyCode, event); }
布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第一个"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第二个"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="第三个"/></LinearLayout>
运行图
Notification主代码
package com.example.mynotification;import android.app.Activity;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class MainActivity extends Activity implements OnClickListener { private Button mButton1; private Button mButton2; private NotificationManager mNotification; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化管理器 mNotification = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); mButton1=(Button) findViewById(R.id.notify1); mButton2=(Button) findViewById(R.id.notify2); mButton1.setOnClickListener(this); mButton2.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.notify1: //初始化Notification 对象 Notification notification=new Notification(); //设置通知的图片 notification.icon=R.drawable.ic_launcher; //设置状态栏文本 notification.tickerText="我是一个消息"; //设置可以取消 notification.flags=Notification.FLAG_AUTO_CANCEL; //设置pendingintent事件 Intent intent=new Intent(getApplicationContext(),MainActivity.class); //设置pendingintent的使用方式 PendingIntent penintent=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT); //设置通知栏的内容 notification.setLatestEventInfo(getApplicationContext(), "我是标题", "我是内容", penintent); //设置时间 notification.when=System.currentTimeMillis();//或者Calendar.getInstance().getTimeInMills(); //通知管理器将通知添加,这里的1要和其他地方对应 mNotification.notify(1,notification); break; case R.id.notify2: mNotification.cancel(1); break; } } }
最新版本
Intent intent=new Intent(getApplicationContext(),MainActivity.class); //设置pendingintent的使用方式 PendingIntent penintent=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT); Notification notification=new Notification.Builder(MainActivity.this).setContentText("我是文本") .setSmallIcon(R.drawable.ic_launcher).setTicker("我是一个消息") .setContentTitle("我是标题").setContentInfo("我是info").setContentIntent(penintent) .setAutoCancel(true).setWhen(System.currentTimeMillis()).build(); mNotification.notify(2,notification);
运行图
自定义的Notification
Intent intent=new Intent(getApplicationContext(),MainActivity.class);//传入自定义的布局文件 RemoteViews remoteView=new RemoteViews(getPackageName(),R.layout.notificatiochun); //设置pendingintent的使用方式 PendingIntent penintent=PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT); Notification notification=new Notification.Builder(MainActivity.this).setContentText("我是文本") .setSmallIcon(R.drawable.ic_launcher).setTicker("我是一个消息") .setContentTitle("我是标题").setContentInfo("我是info").setContentIntent(penintent) .setAutoCancel(true).setWhen(System.currentTimeMillis()).setContent(remoteView).build(); mNotification.notify(2,notification);
0 0
- DatePickerDialog、TimePickerDialog、PopupWindow、Notification
- 自定义Dialog;Notification;PopupWindow;DatePickerDialog;TimePickerDialog
- 自定义Dialog,DatePickerDialog,TimePickerDialog,PopupWindow,Notification
- 0828Android基础自定义Dialog+DatePickerDialog+TimePickerDialog+PopupWindow+Notification
- PopupWindow,DatePickerDialog,TimePickerDialog,ProgressDialog的使用
- 15/8/28/自定义AlterDialog/DatePickerDialog/TimePickerDialog/PopupWindows/Notification
- Android DatePickerDialog和TimePickerDialog
- DatePickerDialog、TimePickerDialog的使用
- AnalogClock和TimePickerDialog、DatePickerDialog
- DatePickerDialog 与TimePickerDialog 应用
- android TimePickerDialog和DatePickerDialog
- 时间对话框。。。datePickerDialog&timePickerDialog
- android *** TimePickerDialog && DatePickerDialog
- <DatePickerDialog>和<TimePickerDialog>
- DatepickerDialog与TimepickerDialog使用
- Android DatePickerDialog,TimePickerDialog
- 对话框之AlertDialog、DatePickerDialog(日期选择对话框)、TimePickerDialog(时间选择)、ProgressDialog(进度)、PopupWindow
- 调用系统的DatePickerDialog,TimePickerDialog
- Ansj标准分词使用注意事项(MyStaticValue.isRealName属性)
- 1033. To Fill or Not to Fill (25)
- 《PCI EXPRESS体系结构导读》读书笔记之PCI总线的存储器读写总线事务
- 在IE10,IE11里面为什么右键菜单会让Acitvex窗口置后。
- 谷歌地图鼠标绘制多边形方法
- DatePickerDialog、TimePickerDialog、PopupWindow、Notification
- 开发不改bug?给你支个招
- 《tomcate权威指南》笔记(持续更新……)
- 学习资料
- linux awk命令详解
- 多线程知识点小结
- mariadb数据库 参数设置
- 机械臂运动学入门(一)
- storyboard的实际用法资料汇总