Android Studio - 日期选择器无法访问以前的日历日期?
创始人
2024-08-16 01:00:20
0

要解决Android Studio中日期选择器无法访问以前的日历日期的问题,可以使用DatePickerDialog的setMinDate方法来限制选择的最小日期。以下是一个代码示例:

import java.util.Calendar;
import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;

public class MainActivity extends AppCompatActivity {

    private DatePickerDialog datePickerDialog;

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

        // 获取当前日期
        Calendar calendar = Calendar.getInstance();
        int currentYear = calendar.get(Calendar.YEAR);
        int currentMonth = calendar.get(Calendar.MONTH);
        int currentDay = calendar.get(Calendar.DAY_OF_MONTH);

        // 创建DatePickerDialog实例
        datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                // 日期选择后的操作
            }
        }, currentYear, currentMonth, currentDay);

        // 设置最小日期为当前日期
        datePickerDialog.getDatePicker().setMinDate(calendar.getTimeInMillis());

        // 显示日期选择器
        datePickerDialog.show();
    }
}

在上面的示例中,我们首先获取当前日期,并将其用作DatePickerDialog的初始日期。然后,我们使用getDatePicker()方法获取DatePickerDialog的内部DatePicker对象,并使用setMinDate()方法将最小日期设置为当前日期。最后,我们调用show()方法显示日期选择器。

通过执行上述代码,您将能够访问以前的日历日期。

相关内容

热门资讯

玩家必看教程!微信微乐自建房辅... 玩家必看教程!微信微乐自建房辅助工具,微乐小程序免费黑科技(开挂自建房)技巧一、微信微乐自建房辅助工...
盘点一款!微乐家乡麻将自建房辅... 盘点一款!微乐家乡麻将自建房辅助app,微乐家乡麻将自建房辅助app(开挂自建房)脚本小薇(辅助器软...
三分钟了解!微乐小程序自建房辅... 三分钟了解!微乐小程序自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)脚本1、每一步都需要思考...
玩家攻略!微信卡五星辅助器,微... 玩家攻略!微信卡五星辅助器,微乐自建房脚本免费入口(开挂自建房)攻略1、打开软件启动之后找到中间准星...
教程辅助!微乐微信自建房辅助工... 教程辅助!微乐微信自建房辅助工具插件,微乐小程序免费黑科技(开挂自建房)工具所有人都在同一条线上,像...
科技新动态!微信雀神麻将挂件开... 科技新动态!微信雀神麻将挂件开挂辅助,微乐小程序免费黑科技(开挂自建房)方法1、在微信雀神麻将挂件开...
教学盘点!微乐自建房辅助工具挂... 教学盘点!微乐自建房辅助工具挂,微乐家乡麻将自建房辅助app(开挂自建房)方法1、微乐家乡麻将自建房...
终于懂了!微乐小程序麻将自建房... 终于懂了!微乐小程序麻将自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)教程1、微乐小...
推荐攻略!微乐自建房辅助工具脚... 推荐攻略!微乐自建房辅助工具脚本,微乐自建房脚本免费入口(开挂自建房)教程推荐攻略!微乐自建房辅助工...
揭秘一下!微乐小程序自建房辅助... 揭秘一下!微乐小程序自建房辅助工具插件,微乐家乡麻将自建房辅助app(开挂自建房)攻略1、微乐小程序...