当前位置: 首页 > 面试题库 >

永久设置DatePickerDialog标题

竺展
2023-03-14
问题内容

我试图永久设置DatePickerDialog标题时遇到问题。

DatePickerFragment.java

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    DatePickerDialog dpd = new DatePickerDialog(getActivity(), this, year, month, day);
    dpd.setTitle("set date");
    return dpd;
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
    // Do something with the date chosen by the user
    }
}

MainActivity.java

public class MainActivity extends FragmentActivity {

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

        Button btn = (Button) findViewById(R.id.button1);

        btn.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                DialogFragment newFragment = new DatePickerFragment();
                newFragment.show(getSupportFragmentManager(), "datePicker");

            }
        });
    }
}

当我单击按钮DatePickerDialog时,对话框标题为“设置日期”,但是当我更改日期时,标题包含选定的日期,而不是“设置日期”。如何永久设置此对话框标题?

经过API 8-10测试。

在此先感谢您,对不起我的英语。帕特里克


问题答案:

如何扩展DatePickerDialog和添加一种setPermanentTitle存储永久性标题的方法,该方法将在日期更改时被强制执行?

public class MyDatePickerDialog extends DatePickerDialog {

    private CharSequence title;

    public MyDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) {
        super(context, callBack, year, monthOfYear, dayOfMonth);
    }

    public void setPermanentTitle(CharSequence title) {
        this.title = title;
        setTitle(title);
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        super.onDateChanged(view, year, month, day);
        setTitle(title);
    }
}

然后使用新setPermanentTitle方法:

    MyDatePickerDialog dpd = new MyDatePickerDialog(this, null, 2012, 10, 10);
    dpd.setPermanentTitle("set date");


 类似资料:
  • 问题内容: 通过MySQL命令行客户端,我试图设置全局mysql_mode: 这适用于当前会话,但是在我重新启动服务器后,sql_mode返回其默认值:’‘,一个空字符串。 如何将sql_mode永久设置为TRADITIONAL? 如果相关,MySQL是WAMP软件包的一部分。 谢谢。 问题答案: 将此添加到您的my.cnf文件(如果使用Windows,则将其添加到my.ini): 并重新启动服务

  • 在Linux上,如何将目录添加到$PATH中,使其在不同会话中保持持久性? 我试图添加一个目录到我的路径,所以它将永远在我的Linux路径。我尝试过: 这是有效的,但是每次我退出终端并启动一个新的终端实例时,这个路径都会丢失,我需要再次运行导出命令。 我怎么做才能永久设置?

  • 问题内容: 我试图将目录添加到我的路径,以便它始终位于我的Linux路径中。我试过了: 这可行,但是每次我退出终端并启动新的终端实例时,此路径都会丢失,因此我需要再次运行export命令。 我该如何做才能将其永久设置? 问题答案: 有多种方法可以做到这一点。实际解决方案取决于目的。 变量值通常存储在分配列表或在系统或用户会话开始时运行的Shell脚本中。如果使用Shell脚本,则必须使用特定的Sh

  • 问题内容: 如何设置“ curl”以在终端中永久使用代理服务器? 问题答案: 您可以在〜/ .bashrc文件中创建别名: 另一个解决方案是使用(也许是更好的解决方案)文件(如果文件不存在,则创建它):

  • 我读过几个关于如何在OSX上永久设置环境变量的答案。

  • 我想从明天开始设置DatePicker对话框的最小日期。如果今天是2015年4月30日,则应从2015年5月1日开始。