android studio简易记事本
时间: 2023-11-25 22:52:38 浏览: 415
Android记事本源代码
5星 · 资源好评率100%
以下是使用Android Studio实现简易记事本的步骤和代码示例:
1.创建一个新的Android Studio项目,并在布局文件中添加一个EditText和三个Button,分别用于添加、更新和删除笔记。
2.创建一个SQLite数据库来存储笔记。在MainActivity.java文件中,创建一个内部类NoteDatabase,该类继承自SQLiteOpenHelper,并实现onCreate()和onUpgrade()方法。
```java
public class MainActivity extends AppCompatActivity {
private EditText etNote;
private Button btnAdd, btnUpdate, btnDelete;
private NoteDatabase dbHelper;
private SQLiteDatabase db;
private Cursor cursor;
private int selectedId = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etNote = findViewById(R.id.et_note);
btnAdd = findViewById(R.id.btn_add);
btnUpdate = findViewById(R.id.btn_update);
btnDelete = findViewById(R.id.btn_delete);
dbHelper = new NoteDatabase(this, "Note.db", null, 1);
db = dbHelper.getWritableDatabase();
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String note = etNote.getText().toString();
ContentValues values = new ContentValues();
values.put("content", note);
db.insert("Note", null, values);
refreshList();
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String note = etNote.getText().toString();
ContentValues values = new ContentValues();
values.put("content", note);
db.update("Note", values, "id=?", new String[]{String.valueOf(selectedId)});
refreshList();
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
db.delete("Note", "id=?", new String[]{String.valueOf(selectedId)});
refreshList();
}
});
refreshList();
}
private void refreshList() {
cursor = db.query("Note", null, null, null, null, null, null);
List<String> notes = new ArrayList<>();
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String content = cursor.getString(cursor.getColumnIndex("content"));
notes.add(id + ": " + content);
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, notes);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
cursor.moveToPosition(position);
selectedId = cursor.getInt(cursor.getColumnIndex("id")); String content = cursor.getString(cursor.getColumnIndex("content"));
etNote.setText(content);
}
});
}
class NoteDatabase extends SQLiteOpenHelper {
public static final String CREATE_NOTE = "create table Note ("
+ "id integer primary key autoincrement, "
+ "content text)";
public NoteDatabase(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_NOTE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
```
3.在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
```
4.运行应用程序并测试它是否可以添加、更新和删除笔记。
阅读全文