每日总结2023/3/7
今天完成了闹钟提醒板块,采用学习了同学的模板思路
首先是工具类,commonuntil类
package com.example.a86191.timeword; import android.app.AlertDialog; import android.content.Context; import android.widget.Toast; import java.text.SimpleDateFormat; import java.util.Date; /* *自定义的通用工具类 */ public class Commonutil { /* *显示短消息 */ public static void showShortMsg(Context context, String msg){ Toast.makeText(context, msg, Toast.LENGTH_SHORT).show(); } /* *显示长消息 */ public static void showLongMsg(Context context, String msg){ Toast.makeText(context, msg, Toast.LENGTH_LONG).show(); } /* *显示消息对话框 */ public static void showDigMsg(Context context, String msg){ new AlertDialog.Builder(context) .setTitle("提示信息") .setMessage(msg) .setPositiveButton("确定",null) .setNegativeButton("取消",null) .create().show(); } public static String getNowDate(){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(new Date()); } }
然后是提醒后的alarmactivity类,此类主要通过此文件进行弹窗提示内容,xml文件由于弹窗遮挡的原因所有不需要进行太多装饰
package com.example.a86191.timeword; import android.content.DialogInterface; import android.os.Bundle; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatActivity; import com.example.a86191.timeword.Commonutil; public class AlarmActivity extends AppCompatActivity { @Override 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("到时间了!"); alert.setMessage(thing); alert.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { } }); alert.show(); } }
这里alarm类的xml文件我们便不进行添加了,下面是clockactivity
package com.example.a86191.timeword; import android.app.AlarmManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.EditText; import android.widget.TimePicker; import com.example.a86191.timeword.Commonutil; import java.util.Calendar; public class ClockActivity extends AppCompatActivity implements View.OnClickListener { private TimePicker timePicker; private EditText et_thing; private String thing; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_clock); //获取时间拾取组件 timePicker = (TimePicker) findViewById(R.id.time1 ); // timePicker.setIs24HourView(true); //设置24小时制 findViewById(R.id.btn_set).setOnClickListener(this); et_thing = findViewById(R.id.et_thing); } @RequiresApi(api = Build.VERSION_CODES.M) @Override public void onClick(View view) { //设置闹钟 thing = et_thing.getText().toString().trim(); Intent intent = new Intent(ClockActivity.this,AlarmActivity.class); Bundle bundle = new Bundle(); bundle.putString("thing",thing); intent.putExtras(bundle); PendingIntent pendingIntent; pendingIntent = PendingIntent.getActivity(ClockActivity.this, 0,intent,PendingIntent.FLAG_UPDATE_CURRENT); 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); Commonutil.showShortMsg(ClockActivity.this,"提醒设置成功!"); } }
clock的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" android:orientation="vertical" android:padding="5dp"> <TimePicker android:id="@+id/time1" 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/btn_set" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="设置闹钟"/> </LinearLayout>