android日期弹出框,Android 日期对话框DatePickerDialog

packagecom.example.yanlei.my;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.view.Menu;importandroid.view.MenuItem;importjava.util.Calendar;importandroid.app.Activity;importandroid.app.DatePickerDialog;importandroid.app.Dialog;importandroid.app.TimePickerDialog;importandroid.os.Bundle;importandroid.view.View;importandroid.widget.Button;importandroid.widget.DatePicker;importandroid.widget.EditText;importandroid.widget.TimePicker;public class MainActivity extendsAppCompatActivity {private Button dateBtn = null;private Button timeBtn = null;private EditText et=null;private final static int DATE_DIALOG = 0;private final static int TIME_DIALOG = 1;private Calendar c = null;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

et=(EditText)findViewById(R.id.et);

dateBtn=(Button) findViewById(R.id.dateBtn);

timeBtn=(Button) findViewById(R.id.timeBtn);

dateBtn.setOnClickListener(newView.OnClickListener(){public voidonClick(View v) {

showDialog(DATE_DIALOG);

}

});

timeBtn.setOnClickListener(newView.OnClickListener(){public voidonClick(View v) {

showDialog(TIME_DIALOG);

}

});

}/*** 创建日期及时间选择对话框*/@Overrideprotected Dialog onCreateDialog(intid) {

Dialog dialog= null;switch(id) {caseDATE_DIALOG:

c=Calendar.getInstance();

dialog= new DatePickerDialog(this,newDatePickerDialog.OnDateSetListener() {public void onDateSet(DatePicker dp, int year,int month, intdayOfMonth) {

et.setText("您选择了:" + year + "年" + (month+1) + "月" + dayOfMonth + "日");

}

}, c.get(Calendar.YEAR),//传入年份

c.get(Calendar.MONTH), //传入月份

c.get(Calendar.DAY_OF_MONTH) //传入天数

);break;caseTIME_DIALOG:

c=Calendar.getInstance();

dialog=new TimePickerDialog(this,newTimePickerDialog.OnTimeSetListener(){public void onTimeSet(TimePicker view, int hourOfDay, intminute) {

et.setText("您选择了:"+hourOfDay+"时"+minute+"分");

}

},

c.get(Calendar.HOUR_OF_DAY),

c.get(Calendar.MINUTE),false);break;

}returndialog;

}

@Overridepublic booleanonCreateOptionsMenu(Menu menu) {//Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.menu_main, menu);return true;

}

@Overridepublic booleanonOptionsItemSelected(MenuItem item) {//Handle action bar item clicks here. The action bar will//automatically handle clicks on the Home/Up button, so long//as you specify a parent activity in AndroidManifest.xml.

int id =item.getItemId();//noinspection SimplifiableIfStatement

if (id ==R.id.action_settings) {return true;

}return super.onOptionsItemSelected(item);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值