每日总结

今日就浅浅的记录一下时间android studio

个控件可以以一个时钟的方式显示计数,也可以是日历形式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AlarmActivity">

    <TimePicker
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <EditText
        android:id="@+id/et_thing"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="请输入待提醒的事宜"/>
    <Button
        android:id="@+id/set_alarm"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="设置闹钟"
        android:textSize="27dp" />
</LinearLayout>

设置闹钟提醒也涉及到intent的使用,intent关联到主函数编辑界面以及闹钟时间到了之后的提示

提示框的出现

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getIntent().getExtras();
        String thing = bundle.getString("thing");
        AlertDialog alert = new AlertDialog.Builder(this).create();
        alert.setTitle("time over!");
        alert.setMessage(thing);
        alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        alert.show();
    }

主函数设置闹钟,用到了闹钟管理器,日历设置时间

    @Override
            public void onClick(View v) {

                String  thing=et_thing.getText().toString().trim();
                System.out.println(thing);
                Intent intent = new Intent(AlarmActivity.this,Alarm.class);
                Bundle bundle = new Bundle();
                bundle.putString("thing",thing);
                intent.putExtras(bundle);
              //  startActivity(intent);
                PendingIntent pendingIntent=PendingIntent.getActivity(AlarmActivity.this,0,intent,PendingIntent.FLAG_IMMUTABLE);
                AlarmManager alarm = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
                Calendar c = Calendar.getInstance();
                //获取在时间拾取器中设置的小时和分钟并给予日历对象
                c.set(Calendar.HOUR_OF_DAY,timePicker.getHour());
                c.set(Calendar.MINUTE,timePicker.getMinute());
                c.set(Calendar.SECOND,0);
                alarm.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent);
                Toast.makeText(AlarmActivity.this, "闹钟设置成功", Toast.LENGTH_SHORT).show();

            }
        });


    }

 

posted @ 2023-03-09 19:45  cojames  阅读(15)  评论(0编辑  收藏  举报