DatePickerDialog、TimePickerDialog、PopupWindow、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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值