记事本在手机上广泛的存在,作为初学者,对该应用进行初步探索,仅以实现增删改查四种主要功能为例进行记录。


一、首先先看一下效果图,依次是主界面,添加笔记页面,内容修改界面。

android 记事本编辑 android实现记事本_android 记事本编辑

 

android 记事本编辑 android实现记事本_android_02

android 记事本编辑 android实现记事本_android 记事本编辑_03

二、用到的知识点:

     1.基本控件的应用,例如:TextView,EditText,Button等;

     2.ListView以及适配器的应用;

     3.数据库的应用;

三、布局界面:

做任何东西,先从布局开始做,把框架搭起来,再进行功能填充。

1.主界面布局 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/back"
    tools:context="personalnotes.mrzhang.com.personalnotes.MainActivity">


    <LinearLayout
        android:layout_width="400dp"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="400dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:textSize="50dp"
            android:textStyle="italic"
            android:textColor="#7eba86"
            android:text="Notes"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:layout_width="315dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/etSearch"
            android:background="@drawable/edit_1"
            />
        <ImageButton
            android:layout_width="50dp"
            android:layout_height="45dp"
            android:id="@+id/ibSearch"
            android:background="@drawable/search_1"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="10000"
        android:orientation="vertical">
        <ListView
            android:id="@+id/lvItems"
            android:layout_width="fill_parent"
            android:layout_height="match_parent">
        </ListView>
    </LinearLayout>

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

        <ImageButton
            android:id="@+id/ibAdd"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_gravity="right"
            android:background="@drawable/add_1" />
    </LinearLayout>
</LinearLayout>

2.添加笔记页面 

activity_content.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back_3"
    android:orientation="vertical"
    tools:context="personalnotes.mrzhang.com.personalnotes.ContentActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10000"
        android:orientation="vertical">
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left|top"
            android:hint="输入标题"
            android:id="@+id/etTittle"
            android:inputType="textMultiLine" />
        <EditText
            android:id="@+id/etContent"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="@null"
            android:gravity="left|top"
            android:inputType="textMultiLine"
            android:hint="输入内容"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        >

        <ImageButton
            android:id="@+id/ibSave"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:layout_gravity="center"
            android:background="@drawable/save" />

    </LinearLayout>
</LinearLayout>

3.内容修改界面 

activity_item.xml

?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/back_3"
    android:orientation="vertical"
    tools:context="personalnotes.mrzhang.com.personalnotes.ItemActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="10000"
        android:orientation="vertical">
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="left|top"
            android:hint="输入标题"
            android:id="@+id/etTittle_1"
            android:inputType="textMultiLine" />
        <EditText
            android:id="@+id/etContent_1"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:background="@null"
            android:gravity="left|top"
            android:inputType="textMultiLine"
            android:hint="输入内容"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical"
        >

        <Button
            android:id="@+id/btnSave_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="保存"/>
    </LinearLayout>
</LinearLayout>

四、功能实现:

   记事本的主要功能无外乎增、删、查、改四种,如果想保存并且实现后期可以查看、修改、保存,就必然用到了数据库,通过对数据库进行增、删、改、查操作来实现记事本的基本功能。在框架搭好的前提下,对每个界面进行功能填充。

1.主界面 MainActivity.java

   主界面主要包含查询搜索框,搜索按钮,litview显示内容框,添加内容按钮,在该文件中将对各部分功能进行填充。

package personalnotes.mrzhang.com.personalnotes;

import...


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageButton ibAdd,ibSearch;
    private EditText etSearch;
    private ListView lvItems;
    private List<Bean> beans;
    private Dao d;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        d = new Dao(this);
        beans = d.findAll();

        ibAdd = (ImageButton) findViewById(R.id.ibAdd);
        ibSearch = (ImageButton) findViewById(R.id.ibSearch);
        lvItems = (ListView) findViewById(R.id.lvItems);
        etSearch = (EditText) findViewById(R.id.etSearch);


        ibAdd.setOnClickListener(this);
        ibSearch.setOnClickListener(this);

        lvItems.setAdapter(new MyAdapter());

        lvItems.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @ Override
            public boolean onItemLongClick(AdapterView<?> arg0, View view, final int position, long arg3) {
                int id_1 = beans.get(position).getId();
                d.delete(id_1);
                beans = d.findAll();
                lvItems.setAdapter(new MyAdapter());
                return true;
            }
        });

        lvItems.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int item,
                                    long position) {

                int id = beans.get(item).getId();
                Intent intent = new Intent(MainActivity.this, ItemActivity.class);
                intent.putExtra("item", String.valueOf(id));
                startActivity(intent);
                finish();

            }
        });

        ibAdd.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,ContentActivity.class);
                startActivity(i);
            }
        });

    }

    @Override
    public void onClick(View v) {
        String str = etSearch.getText().toString();

        switch (v.getId()){
            case R.id.ibSearch:
                    if(str != null) {
                        Intent i = new Intent(MainActivity.this, ItemActivity.class);
                        i.putExtra("search", str);
                        startActivity(i);
                    }
                   else{
                        Toast.makeText(MainActivity.this,"没有查询到相关内容", Toast.LENGTH_SHORT).show();
                    }
        }
    }

    /*
    适配器:listview 和数据库之间
     */

           private class MyAdapter extends BaseAdapter{

        /*
        控制listview中一共有多少个条目
         */

        @Override
        public int getCount() {
            return beans.size();//条目个数 == 集合的size
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Bean bean = beans.get(position);
            View view = View.inflate(MainActivity.this,R.layout.list_item, null);
            TextView tvContent = (TextView) view.findViewById(R.id.tvContent);
            tvContent.setText("内容:" + bean.getContent());

            TextView tvTittle = (TextView) view.findViewById(R.id.tvTittle);
            tvTittle.setText("标题:" + bean.getName());
            return view;

        }
    }

    @Override
    protected void onResume() {
        beans = d.findAll();
        lvItems.setAdapter(new MyAdapter());
        super.onResume();
    }
    /*
    右上角菜单使用
     */

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu,menu);//这里是调用menu文件夹中的main.xml,在登陆界面label右上角的三角里显示其他功能
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id){
            case R.id.action_add:
                Intent i = new Intent(MainActivity.this,ContentActivity.class);
                startActivity(i);
                return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

2.内容添加界面 

ContentActivity.java

在该文件中将对各部分功能进行填充。

package personalnotes.mrzhang.com.personalnotes;

import ...

public class ContentActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageButton ibSave;
    private EditText etTittle,etContent;
    private Dao d;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_content);

        d = new Dao(getBaseContext());

        /*
        绑定id
         */

        ibSave = (ImageButton) findViewById(R.id.ibSave);
        etTittle = (EditText) findViewById(R.id.etTittle);
        etContent = (EditText) findViewById(R.id.etContent);


        ibSave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        String content = etContent.getText().toString();
        String tittle =  etTittle.getText().toString();
        switch (v.getId()) {
            case R.id.ibSave:
                d.add(tittle,content);
                finish();
                break;
        }
    }
}

3.内容显示界面 

ItemActivity.java

 内容显示界面主要组成部分与内容添加界面基本一致,为区分两者,特在保存按钮处进行了区分。

import ...

public class ItemActivity extends AppCompatActivity implements View.OnClickListener{

    private Bean b_1,b_2,b_3;
    private Dao d_1;
    private EditText etTittle_1,etContent_1;
    private Button btnSave_1;
    private int d;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //无title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , WindowManager.LayoutParams. FLAG_FULLSCREEN);
        setContentView(R.layout.activity_item);

        d_1 = new Dao(getBaseContext());
        etTittle_1 = (EditText) findViewById(R.id.etTittle_1);
        etContent_1 = (EditText) findViewById(R.id.etContent_1);
        btnSave_1 = (Button) findViewById(R.id.btnSave_1);
        btnSave_1.setOnClickListener(this);

        /*
        通过搜索跳转到内容界面,如果有修改操作会更新内容后显示
         */

        Intent intent_1 = getIntent();
        String str = intent_1.getStringExtra("search");

        if (str != null){
            b_3 = d_1.find(str);
            etTittle_1.setText(b_3.getName());
            etContent_1.setText(b_3.getContent());
            d = b_3.getId();
        }

        /*
        通过点击item跳转到内容界面,如果有修改操作更新内容后显示
         */
        Intent intent = getIntent();
        String temp = intent.getStringExtra("item");

        if (temp != null) {

            int result = Integer.valueOf(temp);
            b_1 = d_1.find(result);
            etContent_1.setText(b_1.getContent());
            etTittle_1.setText(b_1.getName());
            d = b_1.getId();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnSave_1:
                String context = etContent_1.getText().toString();
                String name = etTittle_1.getText().toString();
                b_2 = new Bean();
                Intent intent = getIntent();
                String temp = intent.getStringExtra("item");
                if (temp!=null){
                    int result = Integer.valueOf(temp);
                    b_2.setId(result);
                    b_2.setName(name);
                    b_2.setContent(context);
                    d_1.update(b_2);
                    Intent i2 = new Intent(ItemActivity.this,MainActivity.class);
                    startActivity(i2);
                    finish();
                }
                else {
                    b_2.setId(d);
                    b_2.setName(name);
                    b_2.setContent(context);
                    d_1.update(b_2);
                    Intent i3 = new Intent(ItemActivity.this,MainActivity.class);
                    startActivity(i3);
                    finish();
                }
                break;
        }
    }
}

4.数据库的辅助类 TextSQLiteOpenHelper.java



package personalnotes.mrzhang.com.personalnotes;

import ...
/**
 * Created by mrzhang on 2017/3/4.
 */

public class TextSQLiteOpenHelper extends SQLiteOpenHelper {
    public TextSQLiteOpenHelper(Context context) {
        super(context, "Text.db", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table Text (id integer primary key autoincrement,content varchar(20),name varchar(20))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}

5.数据库的操作类 Dao.java



package personalnotes.mrzhang.com.personalnotes;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mrzhang on 2017/3/4.
 */

public class Dao {
    private TextSQLiteOpenHelper helper;

    public Dao(Context context) {
        helper = new TextSQLiteOpenHelper(context);
    }

    //添加
    public void add(String name, String content) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("insert into Text (name,content)values(?,?)", new Object[]{name, content});
        db.close();

    }

    public void delete(Integer id){
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("delete from Text where id=?",new Object[]{id});
        db.close();
    }

    public Bean find(int id) {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM Text WHERE id = ?",
                new String[]{String.valueOf(id)});
        //存在数据才返回true
        if (cursor.moveToFirst()) {
            String content = cursor.getString(cursor.getColumnIndex("content"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            return new Bean(content,name);
        }
        cursor.close();
        return null;
    }


    public void update (Bean b){
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL("update Text set content=? ,name=? where id=?",new Object[]{b.getContent(),b.getName(),b.getId()});
        db.close();
    }
    public List<Bean> findAll() {
        SQLiteDatabase db = helper.getReadableDatabase();
        List<Bean> persons = new ArrayList<Bean>();
        Cursor cursor = db.rawQuery("select id,content,name from Text", null);
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            Bean p = new Bean(id, name, content);
            persons.add(p);
        }
        cursor.close();
        db.close();
        return persons;

    }
    public Bean find(String name){
        SQLiteDatabase db = helper.getWritableDatabase();
        Cursor cursor = db.rawQuery("select name,content,id from Text where name=?", new String[]{name});
        while (cursor.moveToNext()){
            String content = cursor.getString(cursor.getColumnIndex("content"));
            String Ming = cursor.getString(cursor.getColumnIndex("name"));
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            return new Bean(id,Ming,content);
        }
        cursor.close();
        db.close();
        return null;
    }
}

6.Dao类 Bean.java


package personalnotes.mrzhang.com.personalnotes;

/**
 * Created by mrzhang on 2017/3/4.
 */

public class Bean {
    private int id;
    private String name;
    private String content;
    //构造方法
    public Bean(String content,String name) {
        this.content=content;
        this.name= name;
    }
    public  Bean(){

    }

    public Bean(int id, String name, String content) {
        this.id = id;
        this.name = name;
        this.content = content;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id=" + id +
                // ", name='" + name + '\'' +
                //", content='" + content + '\'' +

                '}';
    }
}

 至此,具备基本功能的记事本小app已经完工,可以测试一下,或者稍加修改,对界面进行美化。