datepickerDialog 스타일 사용자 정의

10059 단어 android
Date Picker Dialog는 말하자면 Alert Dialog에 Date Picker 하나와 button 두 개를 넣었다는 것이다.
시스템의DatePickerDialog는 주제를 지정했습니다. 저는 button의 스타일을 사용자 정의하고 싶어서 원본 코드에 따라 DatePickerDialog를 다시 써서 그에게 자신의 레이아웃 파일을 추가했습니다.
잡담은 그만두고 코드를 올려라.
MyDatePickerDialog.java
여기의 MyDatePickerDialog는 DatePickerDialog에서 사용하는 것과 같습니다.
package com.example.datepickerdemo.datepickerdemo;

import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

import java.util.Calendar;

public class MyDatePickerDialog extends AlertDialog implements
        DatePicker.OnDateChangedListener {

    private static final String YEAR = "year";
    private static final String MONTH = "month";
    private static final String DAY = "day";

    private final DatePicker mDatePicker;
    private final OnDateSetListener mDateSetListener;
    private final Calendar mCalendar;

    private boolean mTitleNeedsUpdate = true;

    private View view;

    /**
     * The callback used to indicate the user is done filling in the date.
     */
    public interface OnDateSetListener {

        /**
         * @param view The view associated with this listener.
         * @param year The year that was set.
         * @param monthOfYear The month that was set (0-11) for compatibility
         *  with {@link java.util.Calendar}.
         * @param dayOfMonth The day of the month that was set.
         */
        void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
    }
    /**
     * @param context The context the dialog is to run in.
     * @param callBack How the parent is notified that the date is set.
     * @param year The initial year of the dialog.
     * @param monthOfYear The initial month of the dialog.
     * @param dayOfMonth The initial day of the dialog.
     */
    public MyDatePickerDialog(Context context,
                            OnDateSetListener callBack,
                            int year,
                            int monthOfYear,
                            int dayOfMonth) {
        this(context, 0, callBack, year, monthOfYear, dayOfMonth);
    }

    /**
     * @param context The context the dialog is to run in.
     * @param theme the theme to apply to this dialog
     * @param listener How the parent is notified that the date is set.
     * @param year The initial year of the dialog.
     * @param monthOfYear The initial month of the dialog.
     * @param dayOfMonth The initial day of the dialog.
     */
    public MyDatePickerDialog(Context context, int theme, OnDateSetListener listener, int year,
                            int monthOfYear, int dayOfMonth) {
        super(context, theme);

        mDateSetListener = listener;
        mCalendar = Calendar.getInstance();

         Context themeContext = getContext();
         LayoutInflater inflater = LayoutInflater.from(themeContext);
          view = inflater.inflate(R.layout.date_picker_dialog, null);
          view.setBackgroundColor(Color.BLUE);
        //setView(view);


//        setButton(BUTTON_POSITIVE, themeContext.getString(R.string.ok), this);
//        setButton(BUTTON_NEGATIVE, themeContext.getString(R.string.cancel), this);
//        setButtonPanelLayoutHint(LAYOUT_HINT_SIDE);

        mDatePicker = (DatePicker) view.findViewById(R.id.datePicker);
        mDatePicker.init(year, monthOfYear, dayOfMonth, this);
      //  mDatePicker.setValidationCallback(mValidationCallback);
        //        ok  
        //setTitle("    :");
        setButton();
    }

//    private void setTitle(String title) {
//    //       title     。
//        ((TextView) view.findViewById(R.id.date_picker_title)).setText(title);
//    }
    private void setButton() {
//                ,           CallBack  (    ,       ,           )       。
        view.findViewById(R.id.date_picker_ok).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mDateSetListener != null) {
                            // Clearing focus forces the dialog to commit any pending
                            // changes, e.g. typed text in a NumberPicker.
                            mDatePicker.clearFocus();
                            mDateSetListener.onDateSet(mDatePicker, mDatePicker.getYear(),
                                    mDatePicker.getMonth(), mDatePicker.getDayOfMonth());
                        }
                    }
                });
        view.findViewById(R.id.date_picker_cancle).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        cancel();
                    }
                });
    }
    public void myShow() {
        //    show  ,      setContentView    show    ,     。
        show();
        setContentView(view);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        mDatePicker.init(year, month, day, this);

    }


    /**
     * Gets the {@link DatePicker} contained in this dialog.
     *
     * @return The calendar view.
     */
    public DatePicker getDatePicker() {
        return mDatePicker;
    }

    /**
     * Sets the current date.
     *
     * @param year The date year.
     * @param monthOfYear The date month.
     * @param dayOfMonth The date day of month.
     */
    public void updateDate(int year, int monthOfYear, int dayOfMonth) {
        mDatePicker.updateDate(year, monthOfYear, dayOfMonth);
    }



    @Override
    public Bundle onSaveInstanceState() {
        final Bundle state = super.onSaveInstanceState();
        state.putInt(YEAR, mDatePicker.getYear());
        state.putInt(MONTH, mDatePicker.getMonth());
        state.putInt(DAY, mDatePicker.getDayOfMonth());
        return state;
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        final int year = savedInstanceState.getInt(YEAR);
        final int month = savedInstanceState.getInt(MONTH);
        final int day = savedInstanceState.getInt(DAY);
        mDatePicker.init(year, month, day, this);
    }


}

레이아웃 파일
dare_picker_dialog.xml



    

    
    

사용:
package com.example.datepickerdemo.datepickerdemo;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

import android.widget.Button;
import android.widget.DatePicker;

import android.widget.Toast;


import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    Calendar calendar = Calendar.getInstance();
    int year = calendar.get(Calendar.YEAR);
    int monthOfYear = calendar.get(Calendar.MONTH);
    int dayOfMonth = calendar.get(Calendar.DAY_OF_MONTH);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        Button test1= (Button) findViewById(R.id.test1);
        test1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                showDatePicker2();
            }
        });
    }



    public void showDatePicker2(){
        MyDatePickerDialog myDatePickerDialog=new MyDatePickerDialog(this,android.R.style.Theme_Holo_Light_Dialog_NoActionBar,
                new MyDatePickerDialog.OnDateSetListener(){

                    @Override
                    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                        int month=monthOfYear+1;
                        Toast.makeText(
                                MainActivity.this,
                                year + "-" + month + "-" + dayOfMonth,
                                Toast.LENGTH_SHORT).show();
                    }
                },year,monthOfYear,dayOfMonth);
        myDatePickerDialog.myShow();
    }
}
구체적인 단추의 스타일은 아직 바꾸지 않았지만 xml을 통해 쓸 수 있다면 바꿀 수 있습니다.
이 글은 아래의 작가를 참고했지만 사용할 때 그가 한 부분을 잘못 써서 성공하지 못했다. 내가 고쳐주었는데 그래도 고마워!!http://blog.csdn.net/wenxuzl99/article/details/16813633

좋은 웹페이지 즐겨찾기