package com.datepicker; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.DatePicker; public class MainActivity extends AppCompatActivity { private Button date_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); date_btn = (Button)findViewById(R.id.date_picker_button); date_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(); intent.setClass(MainActivity.this, DatePickerActivity.class); startActivity(intent); } }); }}
对应的DatePickerActivity.java文件代码是:
package com.datepicker; import android.app.Activity; import android.os.Bundle; import android.support.v4.app.BundleCompat; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import android.widget.DatePicker; /** * Created by Administrator on 2017/3/16. */ public class DatePickerActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("CheckBoxActivity"); setContentView(R.layout.date_picker); DatePicker datepicker = (DatePicker)this.findViewById(R.id.date_picker); datepicker.init(2010,5,17,null); } }main.xml下布局为:<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.datepicker.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/date_picker_button" android:text="日期" android:gravity="center" android:layout_centerHorizontal="true" android:background="@color/colorAccent" /> </RelativeLayout>对应datepicker下的xml文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <DatePicker android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/date_picker" android:layout_gravity="center_horizontal"> </DatePicker> </LinearLayout>
安卓DatePicker_Demo
最新推荐文章于 2024-04-01 20:01:21 发布