用Android studio写一个记事本软件具有增删改查和定时提醒功能
时间: 2023-10-05 09:04:21 浏览: 173
好的,我会为您提供一个简单的记事本应用程序的代码,具有增删改查和定时提醒功能。请注意,这个应用程序不包括用户身份验证或数据加密等安全特性,仅用于演示目的。
首先,您需要创建一个新的Android项目,并在布局文件中添加以下内容:
activity_main.xml:
```
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/et_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Type your note here"/>
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/et_note"
android:text="Save"/>
<ListView
android:id="@+id/lv_notes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_save"/>
</RelativeLayout>
```
接下来,在MainActivity.java文件中添加以下代码:
```
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Calendar;
public class MainActivity extends AppCompatActivity {
private EditText mEtNote;
private Button mBtnSave;
private ListView mLvNotes;
private ArrayList<String> mNotes;
private ArrayAdapter<String> mAdapter;
private AlarmManager mAlarmManager;
private PendingIntent mAlarmIntent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEtNote = findViewById(R.id.et_note);
mBtnSave = findViewById(R.id.btn_save);
mLvNotes = findViewById(R.id.lv_notes);
mNotes = new ArrayList<>();
mAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, mNotes);
mLvNotes.setAdapter(mAdapter);
// 点击ListView中的项目,将文本设置为EditText中的文本
mLvNotes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mEtNote.setText(mNotes.get(position));
}
});
// 长按ListView中的项目,删除该项目
mLvNotes.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
mNotes.remove(position);
mAdapter.notifyDataSetChanged();
return true;
}
});
// 点击Save按钮,将EditText中的文本保存到ArrayList中,并清除EditText
mBtnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String note = mEtNote.getText().toString();
if (!note.isEmpty()) {
mNotes.add(note);
mAdapter.notifyDataSetChanged();
mEtNote.setText("");
}
}
});
// 设置定时提醒
mAlarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlarmReceiver.class);
mAlarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 0);
mAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mAlarmIntent);
}
}
```
接下来,创建一个广播接收器AlarmReceiver.java,用于处理定时提醒:
```
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "It's time to check your notes!", Toast.LENGTH_SHORT).show();
}
}
```
最后,将广播接收器添加到AndroidManifest.xml文件中:
```
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<receiver android:name=".AlarmReceiver" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
```
运行应用程序,您应该会看到一个具有增删改查和定时提醒功能的记事本应用程序。
阅读全文