基于Android平台开发,备忘录记事本,新建记事本实现(三)

相关视频教程在某站上面(🔍浩宇软件开发)

1. 涉及到的技术点

  1. 卡片布局CardView的使用
  2. SQLite数据库的使用
  3. 实现增,删,改,查

2. 代码实现过程

  1. 新建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>
  1. 新建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());
        }

    }
}

3. 运行效果图

请添加图片描述

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩宇软件开发

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值