DatePickerDialog、TimePickerDialog、PopupWindow、Notification

来源:互联网 发布:ico透明图标制作软件 编辑:程序博客网 时间:2024/04/30 12:08

  • Dialog
    • DatePickerDialog
      • 运行图
    • TimePickerDialog
      • 结果图
    • PopupWindow
      • 布局文件
      • 运行图
    • Notification主代码
      • 最新版本
      • 运行图
    • 自定义的Notification

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();

结果图

这里写图片描述

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
原创粉丝点击