TimePicker,DatePicker一起使用

来源:互联网 发布:淘宝客服提成是几个点 编辑:程序博客网 时间:2024/04/29 22:17

DatePicker布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="match_parent"    android:layout_height="match_parent">    <DatePicker        android:id="@+id/pick"        android:calendarTextColor="@color/black"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:calendarViewShown="false">    </DatePicker></LinearLayout>

//TimePicker布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"><TimePicker    android:id="@+id/time"    android:calendarTextColor="@color/black"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@color/black"    ></TimePicker></LinearLayout>
public class MainActivity extends Activity {    public final static int NOTIFICATION_ID = "NotificationActivityDemo".hashCode();    Button button;    //以dialog形式展现    AlertDialog TimeDialog;    AlertDialog DateDialog;    Calendar calendar = Calendar.getInstance();    int currentYear = calendar.get(Calendar.YEAR);    int currentMonth = calendar.get(Calendar.MONTH);    int currentDay = calendar.get(Calendar.DAY_OF_MONTH);    int currentHour = calendar.get(Calendar.HOUR_OF_DAY);    int currentMinte = calendar.get(Calendar.MINUTE);    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.btn);        innit();    }    private void innit() {        final View dateView = View.inflate(getApplicationContext(),R.layout.data,null);        final View timeView = View.inflate(getApplicationContext(),R.layout.time,null);        TimePicker timePicker = (TimePicker) timeView.findViewById(R.id.time);        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {            @Override            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {                currentHour = hourOfDay;                currentMinte = minute;            }        });        DatePicker datePicker = (DatePicker) dateView.findViewById(R.id.pick);        datePicker.setDrawingCacheBackgroundColor(getResources().getColor(R.color.colorAccent));        datePicker.init(currentYear, currentMonth, currentDay, new DatePicker.OnDateChangedListener() {            @Override            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {                currentYear = year;                currentMonth = monthOfYear;                currentDay = dayOfMonth;            }        });        DateDialog = new AlertDialog.Builder(MainActivity.this)                .setView(dateView)                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        Toast.makeText(MainActivity.this,currentYear+"年"+currentMonth+"月"+currentDay+"日"+currentHour+"时"+currentMinte+"分",Toast.LENGTH_LONG).show();                    }                })                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        DateDialog.dismiss();                    }                })                .create();        TimeDialog = new AlertDialog.Builder(MainActivity.this)                .setView(timeView)                .setPositiveButton("确定", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        DateDialog.show();                    }                })                .setNegativeButton("取消", new DialogInterface.OnClickListener() {                    @Override                    public void onClick(DialogInterface dialog, int which) {                        TimeDialog.dismiss();                    }                })                .create();        button.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                TimeDialog.show();            }        });    }}
0 0
原创粉丝点击