相关视频教程在某站上面(🔍浩宇软件开发)
1. 涉及到的技术点
- 卡片布局CardView的使用
- SQLite数据库的使用
- 实现增,删,改,查
2. 代码实现过程
- 新建activity_create_note.xml布局
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".CreateNoteActivity">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:navigationIcon="@drawable/baseline_arrow_back_24"
app:title="新建备忘录" />
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:orientation="vertical">
<EditText
android:id="@+id/et_note_title"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@null"
android:hint="标题..."
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#f5f5f5" />
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_marginTop="16dp"
android:backgroundTint="#f5f5f5">
<EditText
android:id="@+id/et_note_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@null"
android:gravity="top"
android:hint="请输入内容..."
android:lineSpacingExtra="6dp"
android:padding="4dp"
android:textSize="13sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:backgroundTint="@color/my_light_primary"
app:cardCornerRadius="25dp">
<Button
android:id="@+id/btn_submit"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="新建" />
</androidx.cardview.widget.CardView>
</androidx.appcompat.widget.LinearLayoutCompat>
</androidx.appcompat.widget.LinearLayoutCompat>
- 新建CreateNoteActivity.java 页面
public class CreateNoteActivity extends BaseActivity {
private EditText et_note_title;
private EditText et_note_content;
private Toolbar toolbar;
private Button btn_submit;
private NoteInfo noteInfo;
@Override
protected int getLayoutResId() {
return R.layout.activity_create_note;
}
@Override
protected void initViews() {
et_note_title = findViewById(R.id.et_note_title);
et_note_content = findViewById(R.id.et_note_content);
toolbar = findViewById(R.id.toolbar);
btn_submit = findViewById(R.id.btn_submit);
}
@Override
protected void setListener() {
//返回
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
btn_submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = et_note_title.getText().toString();
String content = et_note_content.getText().toString();
if (TextUtils.isEmpty(title) || TextUtils.isEmpty(content)) {
Toast.makeText(CreateNoteActivity.this, "请完善信息~~", Toast.LENGTH_SHORT).show();
} else {
UserInfo userInfo = UserInfo.getUserInfo();
if (noteInfo == null) {
int row = NoteDbHelper.getInstance(CreateNoteActivity.this).createNote(userInfo.getUsername(), title, content);
if (row > 0) {
Toast.makeText(CreateNoteActivity.this, "创建成功~~", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(CreateNoteActivity.this, "创建失败~~", Toast.LENGTH_SHORT).show();
}
} else {
int row = NoteDbHelper.getInstance(CreateNoteActivity.this).editNote(noteInfo.getNote_id(), title, content);
if (row > 0) {
Toast.makeText(CreateNoteActivity.this, "修改成功~~", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(CreateNoteActivity.this, "修改成功~~", Toast.LENGTH_SHORT).show();
}
}
setResult(1000);
finish();
}
}
});
}
@Override
protected void initData() {
//跳转传值
noteInfo = (NoteInfo) getIntent().getSerializableExtra("noteInfo");
if (noteInfo != null) {
toolbar.setTitle("编辑备忘录");
btn_submit.setText("编辑");
et_note_title.setText(noteInfo.getNote_title());
et_note_content.setText(noteInfo.getNote_content());
}
}
}