您的位置:首页 > 移动开发 > Android开发

Android控件之TimePickerDialog、DatePickerDialog和自定义DialogPopupWindow与NotificationManager

2015-08-30 20:42 621 查看

概述

TimePickerDialog与DatePickerDialog: 用于调整时间的控件。

自定义dialog: 可以自定义显示的界面布局和内容。

PopupWindow的常用方法有:

setWidth()、setHight()、setFocusable()、setContentView()、showAsDropDown()、setBackgroundDrawable()。

Notification:

有新、老两种方法。NotificationManager用于管理Notification。

老方法:

Notification.XXX:常用属性有:icon用于设置图片,tickerText用于设置提示内容,flags用于设置标志。

新方法jdk1.6后

Buider(活动名,this);

setTicker():设置提示标题,setContentView():设置自定义界面,setAutoCancel():是否能通过点击外部取消界面,setShowWhen():设置显示时间。

内容

DatePickerDialog与TimePickerDialog

private Calendar mCalendar;//全局变量


private void datePickerDialog() {
        mCalendar = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(DialogActivity.this, new DatePickerDialog.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));
        dialog.show();
}


private void timePickerDialog() {
       mCalendar = Calendar.getInstance();
       TimePickerDialog dialog = new TimePickerDialog(DialogActivity.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 format = new SimpleDateFormat("yyyy年MM月dd日HH:mm");
               Toast.makeText(getApplicationContext(),format.format(mCalendar.getTime()),Toast.LENGTH_SHORT).show();
           }
       },mCalendar.get(Calendar.HOUR),mCalendar.get(Calendar.MINUTE),true);
       dialog.show();
   }


结果演示:



自定义Dialog

public void customDialog(){
        mDialog = new Dialog(DialogActivity.this,R.style.DialogNoTitle);

        LayoutInflater inflater = getLayoutInflater();
        final View dialogView = inflater.inflate(R.layout.layout_custom_dialog, null);
        TextView textViewContent = (TextView)dialogView.findViewById(R.id.text_dialog_content);
        Button button_cancel = (Button)dialogView.findViewById(R.id.button_cancle);
        Button button_ok = (Button)dialogView.findViewById(R.id.button_ok);
        textViewContent.setText("重新设定的内容");
        button_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDialog.dismiss();
            }
        });
        button_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getApplicationContext(), "您选择了ok", Toast.LENGTH_SHORT).show();
                mDialog.dismiss();
            }
        });
        //mDialog.setTitle("我是标题");
        // mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.setContentView(dialogView);
        mDialog.show();
    }


Dialog的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/dialog_bg">
    <TextView
        android:id="@+id/text_dialog_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="@drawable/dialog_title_bg"
        android:padding="10dp"
        android:text="我是标题"/>
    <TextView
        android:id="@+id/text_dialog_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="20dp"
        android:text="我是内容"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/button_cancle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/btn_state"
            android:text="cancel"/>
        <Button
            android:id="@+id/button_ok"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/right_btn_state"
            android:text="ok"/>
    </LinearLayout>
</LinearLayout>


需要在value里面写的文件

btn_nomal

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomLeftRadius="@dimen/corners"></corners>
    <solid android:color="#79dff6"></solid>
    <stroke android:color="#fff" android:width="@dimen/stroke_width"></stroke>
</shape>


btn_pressed

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomLeftRadius="@dimen/corners"></corners>
    <solid android:color="#239fff"></solid>
    <stroke android:color="#fff" android:width="@dimen/stroke_width"></stroke>
</shape>


btn_right_nomal

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomRightRadius="@dimen/corners"></corners>
    <solid android:color="#79dff6"></solid>
    <stroke android:color="#fff" android:width="@dimen/stroke_width"></stroke>
</shape>


btn_right_pressed

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:bottomRightRadius="@dimen/corners"></corners>
    <solid android:color="#239fff"></solid>
    <stroke android:color="#fff" android:width="@dimen/stroke_width"></stroke>
</shape>


btn_state

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/btn_nomal" ></item>
    </selector>


right_btn_state

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/btn_right_pressed" android:state_pressed="true"></item>
    <item android:drawable="@drawable/btn_right_nomal" ></item>
</selector>


dialog_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="@dimen/corners"></corners>
    <solid android:color="#ffffff"></solid>
    <stroke android:color="#fff" android:width="2dp"></stroke>
</shape>


dialog_title_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:topLeftRadius="@dimen/corners"
        android:topRightRadius="@dimen/corners"></corners>
    <solid android:color="#79dff6"></solid>
    <stroke android:color="#fff" android:width="@dimen/stroke_width"></stroke>
</shape>


演示:



PopupWindow

private PopupWindow mPopupWindow;//设定为全局变量


private void showPopupWindow(){
        mPopupWindow = new PopupWindow(DialogActivity.this);
        View popView = getLayoutInflater().inflate(R.layout.layout_popup_window,null);
        mPopupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        mPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setContentView(popView);
        mPopupWindow.setOutsideTouchable(true);
        mPopupWindow.setFocusable(false);
        mPopupWindow.showAsDropDown(dialogButton2);

    }


popupWindow的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:background="@drawable/dialog_bg">
    <TextView
        android:id="@+id/text1_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="文本1"/>
    <TextView
        android:id="@+id/text2_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="文本2"
        android:layout_margin="20dp"/>
    <TextView
        android:id="@+id/text3_popup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:text="文本3"/>
</LinearLayout>


点击后退便能推迟PopupWindow,需要加一段代码

@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK) {
            if(mPopupWindow!=null&&mPopupWindow.isShowing()){
                mPopupWindow.dismiss();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);

    }


结果演示:



Notification

package com.example.administrator.myexample;

import android.annotation.TargetApi;
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.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RemoteViews;

/**
 * Created by Administrator on 2015/8/28.
 */
public class NotificationActivity extends Activity implements View.OnClickListener{
    private Button mButtonNotify1;
    private Button mButtonNotify2;
    private Button mButtonNotify3;
    private Button mButtonNotify4;
    private NotificationManager mNotificationManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_notifycation);

        mNotificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

        mButtonNotify1 = (Button)findViewById(R.id.button1_notify);
        mButtonNotify2 = (Button)findViewById(R.id.button2_notify);
        mButtonNotify3 = (Button)findViewById(R.id.button3_notify);
        mButtonNotify4 = (Button)findViewById(R.id.button4_notify);

        mButtonNotify1.setOnClickListener(this);
        mButtonNotify2.setOnClickListener(this);
        mButtonNotify3.setOnClickListener(this);
        mButtonNotify4.setOnClickListener(this);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button1_notify:
                oldNotification();
                break;
            case R.id.button2_notify:
                mNotificationManager.cancel(1);
                break;
            case R.id.button3_notify:
                newNotification();
                break;
            case R.id.button4_notify:
                customNotification();
                break;
            default:
                break;
        }
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    private void newNotification() {
        Intent intent = new Intent(getApplicationContext(),NotificationActivity.class);
        PendingIntent pend = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
        Notification notification = new Notification.Builder(NotificationActivity.this).setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("我是一条消息").setContentTitle("我是标题").setContentText("我是文本")
                .setContentInfo("我是info").setContentIntent(pend).setAutoCancel(true)
                .setWhen(System.currentTimeMillis()).build();
        mNotificationManager.notify(2,notification);
    }

    private void oldNotification() {
        Notification notification = new Notification();
        notification.icon = R.mipmap.ic_launcher;
        notification.tickerText = "我是一个消息";
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        Intent intent = new Intent(getApplicationContext(),NotificationActivity.class);
        PendingIntent pend = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
        notification.setLatestEventInfo(getApplicationContext(),"我是标题","我是内容",pend);
        notification.when = System.currentTimeMillis()+2000;
        mNotificationManager.notify(1,notification);
    }

    private void customNotification() {
        RemoteViews remoteViews = new RemoteViews(getPackageName(),R.layout.custom_notification);
        Intent intent = new Intent(getApplicationContext(),NotificationActivity.class);
        PendingIntent pend = PendingIntent.getActivity(getApplicationContext(), 1, intent, PendingIntent.FLAG_ONE_SHOT);
        Notification notification = new Notification.Builder(NotificationActivity.this).setSmallIcon(R.mipmap.ic_launcher)
                .setTicker("我是一条消息").setContentTitle("我是标题").setContentText("我是文本")
                .setContentInfo("我是info").setContentIntent(pend).setAutoCancel(true)
                .setWhen(System.currentTimeMillis()).setContent(remoteViews).build();
        mNotificationManager.notify(3,notification);
    }
}


布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <Button
        android:id="@+id/button1_notify"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发出通知(旧法)"/>
    <Button
        android:id="@+id/button2_notify"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="取消通知"/>
    <Button
        android:id="@+id/button3_notify"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="发送消息(新法)"/>

</LinearLayout>


结果演示:




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: