2

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

4

3 回答 3

2

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

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

来源:文档

但是,您可以轻松扩展现有的并创建自己的.

此外,如果您只是愿意拉皮条用户界面,您可以看看DateSlider

于 2013-01-12T10:20:04.390 回答
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;
}
}
于 2013-01-12T10:39:23.233 回答
1

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

于 2017-08-31T08:08:39.437 回答