其他分享
首页 > 其他分享> > Android 实现日期选择器

Android 实现日期选择器

作者:互联网

参考

代码如下

public class MainActivity extends Activity {

    private TextView getTime;
    private Calendar calendar;// 用来装日期的
    private DatePickerDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getTime = (TextView) findViewById(R.id.time);
        getTime.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                calendar = Calendar.getInstance();
                dialog = new DatePickerDialog(MainActivity.this,
                        new DatePickerDialog.OnDateSetListener() {

                            @Override
                            public void onDateSet(DatePicker view, int year,
                                    int monthOfYear, int dayOfMonth) {
                                System.out.println("年-->" + year + "月-->"
                                        + monthOfYear + "日-->" + dayOfMonth);
                                getTime.setText(year + "/" + monthOfYear + "/"
                                        + dayOfMonth);
                            }
                        }, calendar.get(Calendar.YEAR), calendar
                                .get(Calendar.MONTH), calendar
                                .get(Calendar.DAY_OF_MONTH));
                dialog.show();
            }
        });

    }
}

布局就一个textview,自己写一下就可以了

标签:getTime,Calendar,private,日期,monthOfYear,calendar,Android,选择器,dayOfMonth
来源: https://blog.csdn.net/weixin_44911775/article/details/122586712