android记事本——编辑页面

实现效果:

在这里插入图片描述
实现功能:
在记事列表页点击右侧“+”按钮,跳转到记事项编辑页面,记事项编辑页面有记事项标题编辑、添加图片(可以不添加)、添加记事具体内容与设置记事项的优先级等输入框。用户根据要求输入对应内容。当用户点击保存按钮,会提取各输入框的内容判断是否全部有输入,若出现输入框无内容,则会提示保存失败,否则提示保存成功并且跳转到记事本列表页。当点击取消按钮,会弹出是否保存的提示框,有取消与保存按钮,点击取消则不会对编辑的内容进行保存,直接跳转页面,点击确定则保存内容到数据库中再跳转到列表显示页面。

xml代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--标题栏-->

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@color/colorPrimary"
        android:orientation="horizontal"
        android:weightSum="1">
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.6"
            android:weightSum="1">
            <TextView
                android:id="@+id/tv_now"
                android:layout_width="wrap_content"
                android:layout_height="49dp"
                android:textColor="#ffffff"
                android:textAlignment="textEnd"
                android:gravity="end|center_vertical"
                android:paddingRight="8dp"
                android:layout_weight="1.50" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:orientation="vertical"

            android:layout_weight="0.4">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:orientation="horizontal"

                android:padding="2dp"
                android:layout_gravity="right">
                <Button
                    android:id="@+id/btn_save"
                    android:layout_width="50dp"
                    android:layout_height="40dp"
                    android:text="@string/btn_save"
                    android:background="#ffffff"
                    android:textColor="@color/colorPrimary"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="2dp"/>
                <Button
                    android:id="@+id/btn_return"
                    android:layout_width="50dp"
                    android:layout_height="40dp"
                    android:text="@string/btn_cancel"
                    android:background="#ffffff"
                    android:textColor="@color/colorPrimary"
                    android:layout_marginRight="8dp"
                    android:layout_marginTop="2dp"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
    <EditText
        android:id="@+id/et_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:drawableLeft="@drawable/title"
        android:drawablePadding="10dp"
        android:textSize="14dp"
        android:hint="标题">
        <requestFocus />
    </EditText>
    <ImageView
        android:id="@+id/pic"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:layout_marginTop="10dp"
        android:layout_marginBottom="10dp"
        android:layout_gravity="center_horizontal"
        app:srcCompat="@drawable/addp"
        />
    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="添加具体事项"
        android:textSize="16dp"
        android:gravity="left"/>

    <LinearLayout
        android:id="@+id/checklist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="right">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginRight="5dp"
            android:text="设置记事优先级"
            android:textSize="14dp"
            />
        <EditText
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:id="@+id/itempre"
            android:textSize="16dp"
            android:textColor="#888"
            android:hint="0~5"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/testpic"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    </LinearLayout>

java代码:

ItemDetailActivity.java

package com.example.lsl.daily_note;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static com.example.lsl.daily_note.Note.deleteAllNote;

/**
 * Created by lsl on 2020/6/8.
 */

public class ItemDetailActivity extends AppCompatActivity {
    EditText mEditSearch;//搜索框
    Button mTvSearch;//搜索按钮
    private Button add;//添加按钮
    private Button delete;//删除按钮
    private Button up;//升序
    private Button down;//降序
    private ListView noteListView;
    private List<NoteInfo> noteList = new ArrayList<>();
    private List<NoteInfo> noteListup = new ArrayList<>();
    private List<NoteInfo> noteListdown = new ArrayList<>();
    private List<NoteInfo> notesearchList = new ArrayList<>();
    private ListAdapter mListAdapter;
    private ListAdapter mListSearchAdapter;

    private static NoteDataBaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item_detail);
        dbHelper = new NoteDataBaseHelper(this,"MyNote.db",null,1);
        Intent intent=getIntent();

        //点击添加按钮跳转页面
        add = (Button) findViewById(R.id.btn_add);
        delete = (Button) findViewById(R.id.btn_delete);

        //先测试添加一条数据
      /*  ContentValues values = new ContentValues();
        values.put(Note.title,"测试笔记");
        values.put(Note.content,"以下为测试内容!!!");
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        values.put(Note.time,sdf.format(date));
        Note.insertNote(dbHelper,values);*/

        initView();
        setListener();

        //跳转回主界面 刷新列表
        Intent intent1 = getIntent();
        if (intent1 != null){
            getNoteList();
            mListAdapter.refreshDataSet();//渲染列表
        }
    }

    //初始化视图
    private void initView(){
        noteListView = (ListView) findViewById(R.id.note_list);
        add = (Button) findViewById(R.id.btn_add);
        mTvSearch = (Button) findViewById(R.id.btn_search);
        mEditSearch = (EditText) findViewById(R.id.edit_search);
        up = (Button) findViewById(R.id.btn_up);
        down = (Button) findViewById(R.id.btn_down);
        //获取noteList
        getNoteList();
        mListAdapter = new ListAdapter(ItemDetailActivity.this,noteList);
        noteListView.setAdapter(mListAdapter);
    }
    //搜索刷新列表
    private void refreshListView() {
        mListAdapter.notifyDataSetChanged();
        noteListView.setAdapter(mListAdapter);
    }

    //设置监听器
private void setListener(){
    delete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            deleteAllNote(dbHelper);
            getNoteList();
            refreshListView();
            mListAdapter.refreshDataSet();
            Toast.makeText(ItemDetailActivity.this,"删除成功!",Toast.LENGTH_LONG).show();

        }
    });
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(ItemDetailActivity.this,NoteEditorActivity.class);
            startActivity(intent);
            ItemDetailActivity.this.finish();
        }
    });

    up.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            queryDataUp();
        }
    });
    down.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            queryDataDown();
        }
    });

    noteListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            NoteInfo noteInfo = noteList.get(position);
            Intent intent = new Intent();
            Bundle bundle = new Bundle();
            bundle.putSerializable("noteInfo",(Serializable)noteInfo);
            intent.putExtras(bundle);
            intent.setClass(ItemDetailActivity.this, NoteEditorActivity.class);
            startActivity(intent);
            ItemDetailActivity.this.finish();
        }
    });
    mTvSearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //隐藏键盘
            ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE))
                    .hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    });
    /**
     * EditText搜索框对输入值变化的监听,实时搜索
     */
    // TODO: 2017/8/10 3、使用TextWatcher实现对实时搜索
    mEditSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}

        @Override
        public void afterTextChanged(Editable editable) {
                String searchString = mEditSearch.getText().toString();
                queryData(searchString);
        }
    });

    noteListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
            final NoteInfo noteInfo = noteList.get(position);
            String title = "提示";
            new AlertDialog.Builder(ItemDetailActivity.this)
                    .setIcon(R.drawable.book)
                    .setTitle(title)
                    .setMessage("确定要删除吗?")
                    .setPositiveButton(R.string.btn_confirm, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Note.deleteNote(dbHelper,Integer.parseInt(noteInfo.getId()));
                            refreshListView();
                            noteList.remove(position);
                            mListAdapter.refreshDataSet();
                            Toast.makeText(ItemDetailActivity.this,"删除成功!",Toast.LENGTH_LONG).show();
                        }
                    })
                    .setNegativeButton(R.string.btn_cancel, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    }).create().show();
            return true;
        }
    });
}
    //从数据库中读取所有笔记 封装成List<NoteInfo>
    private void getNoteList(){
        noteList.clear();
        Cursor allNotes = Note.getAllNotes(dbHelper);
        noteInfoSet(noteList,allNotes);
    }
    private void noteInfoSet(List<NoteInfo> noteinfo,Cursor Notes){
        for (Notes.moveToFirst(); !Notes.isAfterLast(); Notes.moveToNext()){
            NoteInfo noteInfo = new NoteInfo();
            noteInfo.setId(Notes.getString(Notes.getColumnIndex(Note._id)));
            noteInfo.setTitle(Notes.getString(Notes.getColumnIndex(Note.title)));
            noteInfo.setContent(Notes.getString(Notes.getColumnIndex(Note.content)));
            noteInfo.setDate(Notes.getString(Notes.getColumnIndex(Note.time)));
            noteInfo.setDes(Notes.getString(Notes.getColumnIndex(Note.content)));
            noteInfo.setPre(Notes.getString(Notes.getColumnIndex(Note.pre)));
            noteInfo.setPhoto(Notes.getBlob(Notes.getColumnIndex(Note.picture)));
            noteinfo.add(noteInfo);
        }
    }
    private void getSearchList(String searchData){
        notesearchList.clear();
        Cursor allNotes = Note.getSearchNotes(dbHelper,searchData);
        noteInfoSet(notesearchList,allNotes);
    }
    private void getListup(){
        noteListup.clear();
        Cursor upNotes = Note.upNotes(dbHelper);
        noteInfoSet(noteListup,upNotes);
    }
    private void getListDown(){
        noteListdown.clear();
        Cursor downNotes = Note.downNotes(dbHelper);
        noteInfoSet(noteListdown,downNotes);
    }

    /**
     * 搜索数据库中的数据
     *
     * @param searchData
     */
    private void queryData(String searchData) {

        getSearchList(searchData);
        mListSearchAdapter = new ListAdapter(ItemDetailActivity.this,notesearchList);
        noteListView.setAdapter( mListSearchAdapter);
        mListSearchAdapter.refreshDataSet();//渲染列表
    }
    private void queryDataUp() {
        getListup();
        mListSearchAdapter = new ListAdapter(ItemDetailActivity.this,noteListup);
        noteListView.setAdapter( mListSearchAdapter);
        mListSearchAdapter.refreshDataSet();//渲染列表
   }
    private void queryDataDown() {
        getListDown();
        mListSearchAdapter = new ListAdapter(ItemDetailActivity.this,noteListdown);
        noteListView.setAdapter( mListSearchAdapter);
        mListSearchAdapter.refreshDataSet();//渲染列表
    }

    //给其他类提供dbHelper
    public static NoteDataBaseHelper getDbHelper() {
        return dbHelper;
    }
}

总结:
这是本人的移动应用课程的期末作业,大概写了一个星期左右,对于Java这门语言只能说是掌握个基础,没有深入的学习,导致代码中出现一些冗余,希望大佬看见不要见笑,对于同样是小白的同学可以借鉴一下。
因为代码量实在是大,所以我没有将主要的代码放上去,需要的同学可以下载(觉得有用就给个星呗)
github:https://github.com/hhhhhhe/Daily-Note
csdn:https://download.csdn.net/download/lsl30522/12562305

  • 9
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
实验目的 综合运用基于android平台的智能移动终端软件开发技术。 实验内容 设计实现一个智能移动终端软件应用,至少包含3个相互关联的应用功能,具有较好的用户界面和实际的应用价值以及合理的功能模块结构。 设计方案 实现一个日记本。分为登录界面、日记列表界面、日记内容界面、日记查找界面、日记新建界面。使用SQLiteDatabase数据库存储日记内容。 首先要有一个LoginActivity,输入密码,点击按钮,判断密码是否正确后,用intent跳转到MainActivity。 MainActivity主要包括activity_main里列表的关联,实现点击列表时跳转到show_content_diary日记内容界面,启动ShowContentDiary活动。同时,MainActivity还实现了菜单的初始化,使用上下文菜单,包括搜索和新建功能。MainActivity里还有Set_refresh_data函数,用于初始化和删除日记之后刷新列表。 Note是一个日记信息类,类似于结构体。里面有set和get方法。 NoteAdapter类继承了ArrayAdapter,相当于是Note数组对象的适配器,用来包装Note数据,很好地实现数据和界面分离。 AddNewDiary这个类用于新建一则日记,使用add_new_diary.xml界面,这里没有实现图片添加功能,只是用Toast说明,除此之外,会自动写好时间、日期,只需要添加标题和内容即可。 要创建一个数据库的帮助类,这样使用起SQLiteDatabase就方便了。这和sql查询语言类似,创建、查询、删除……
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值