【问题标题】:Customizing date picker dialog android自定义日期选择器对话框android
【发布时间】:2013-01-12 10:06:55
【问题描述】:

我想更改日期选择器对话框的背景、文本颜色和字体,我也想更改自定义正面和负面按钮。这怎么可能?

【问题讨论】:

标签: android android-datepicker


【解决方案1】:

从对话框中选择日期并在 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】:

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

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

    来源:Documentation

    不过,您可以轻松extend the existing one and create your own

    另外,如果你只是想改进你的 UI,你可以看看DateSlider

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 2023-03-30
        • 1970-01-01
        相关资源
        最近更新 更多