记账本APP8

今天将时间这一个功能给写一下,点击输入时间的文本框,会弹出一个日历

AddActivity.java代码如下:

复制代码
 1 public class AddActivity extends AppCompatActivity {
 2 
 3     //账单时间
 4     private EditText medittime;
 5 
 6     @Override
 7     protected void onCreate(Bundle savedInstanceState) {
 8         super.onCreate(savedInstanceState);
 9         setContentView(R.layout.activity_add);
10 
11         //时间
12         medittime = findViewById(R.id.edittime);
13         medittime.setOnTouchListener(new View.OnTouchListener() {
14             @Override
15             public boolean onTouch(View v, MotionEvent event) {
16                 if (event.getAction() == MotionEvent.ACTION_DOWN) {
17                     showDataPickDlg();
18                     return true;
19                 }
20                 return false;
21             }
22         });
23         medittime.setOnFocusChangeListener(new View.OnFocusChangeListener() {
24             @Override
25             public void onFocusChange(View v, boolean hasFocus) {
26                 if (hasFocus) {
27                     showDataPickDlg();
28                 }
29             }
30         });
31 
32 }
33 //时间
34     protected void showDataPickDlg(){
35         Calendar calendar=Calendar.getInstance();
36         DatePickerDialog dataPickerDialog=new DatePickerDialog(AddActivity.this, new DatePickerDialog.OnDateSetListener() {
37             @Override
38             public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
39                 AddActivity.this.medittime.setText(year+"-"+(month+1)+"-"+dayOfMonth);
40             }
41         },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
42         dataPickerDialog.show();
43     }
复制代码

 

 
posted @ 2021-03-02 19:14  一口獠牙  阅读(46)  评论(0编辑  收藏  举报