第四周总结
时间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();
}
});
}
对菜单栏里添加更多按钮
首先新建一个menu布局文件
调用以下函数实现删除
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.delete:
//new AlertDialog.Builder(EditActivity.class)
new AlertDialog.Builder(EditActivity.this)
.setMessage("确认删除吗?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(openMode==4){
intent.putExtra("mode",-1);
setResult(RESULT_OK,intent);
}else {
intent.putExtra("mode",2);
intent.putExtra("id",id);
setResult(RESULT_OK,intent);
}
finish();
}
}).setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).create().show();
break;
}
return super.onOptionsItemSelected(item);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)