自定义日期选择器对话框android

时间:2013-01-12 10:06:55

标签: android android-datepicker

我想更改datepicker对话框的背景,文字颜色和字体以及它我想要更改自定义正面和负面按钮..它怎么可能?

3 个答案:

答案 0 :(得分:2)

  

如果预构建的小部件或布局都不符合您的需求,您可以创建自己的View子类。如果您只需要对现有的小部件或布局进行小的调整,您可以简单地对小部件或布局进行子类化并覆盖其方法。

     

创建自己的View子类可以精确控制   屏幕元素的外观和功能。

来源:Documentation

但是,您可以轻松extend the existing one and create your own

此外,如果您只是想让您的用户界面变得模糊,那么您可以查看DateSlider

答案 1 :(得分:2)

从对话框中选择日期并在TextView中显示。

public class DatePickerDemoActivity extends Activity {
/** Called when the activity is first created. */

   private TextView mDateDisplay;
    private Button mPickDate;
    private int mYear;
    private int mMonth;
    private int mDay;

    static final int DATE_DIALOG_ID = 0;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // capture our View elements
    mDateDisplay = (TextView) findViewById(R.id.dateDisplay);
    mPickDate = (Button) findViewById(R.id.pickDate);

    // add a click listener to the button
    mPickDate.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            showDialog(DATE_DIALOG_ID);
        }
    });

    // get the current date
    final Calendar c = Calendar.getInstance();
    mYear = c.get(Calendar.YEAR);
    mMonth = c.get(Calendar.MONTH);
    mDay = c.get(Calendar.DAY_OF_MONTH);

    // display the current date (this method is below)
    updateDisplay();
}

// updates the date in the TextView
private void updateDisplay() {
    mDateDisplay.setText(getString(R.string.strSelectedDate,
        new StringBuilder()
                // Month is 0 based so add 1
                .append(mMonth + 1).append("-")
                .append(mDay).append("-")
                .append(mYear).append(" ")));
}

// the callback received when the user "sets" the date in the dialog
private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {

            public void onDateSet(DatePicker view, int year,
                                  int monthOfYear, int dayOfMonth) {
                mYear = year;
                mMonth = monthOfYear;
                mDay = dayOfMonth;
                updateDisplay();
            }
        };

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case DATE_DIALOG_ID:
        return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
                mDay);
    }
    return null;
}
}

答案 2 :(得分:1)

如果你想要微调器类型的日期选择器,那么this一个是好的。你可以自定义everthing