Android 开发简单记事本程序(附源码)

    简单介绍一下功能:简单记事本只能添加文字内容,首页用ListView显示所有保存的事项和保存的时间,添加页面添加内容,点击首页的ListView可以查看内容,删除内容。

    先看一下运行效果:

先建立数据库:

package com.engineer.shizhibin.note;

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

public class NoteDb extends SQLiteOpenHelper {
    public static final String TABLE_NAME = "notes";
    public static final String CONTENT = "content";
    public static final String ID = "_id";
    public static final String TIME = "time";
    public NoteDb(Context context) {
        super(context, "notes", null,1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql ="create table "+TABLE_NAME+" ( "+ID+" integer primary key AUTOINCREMENT, "+CONTENT
                +" TEXT NOT NULL, "+TIME+" TEXT NOT NULL )";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

 

然后是MainActivity:

 

    有显示目录的ListView,和添加按钮。他们都要设置点击监听事件。点击添加 跳转到添加文字的activity 点击ListView跳转到Show Activity。

 

onResume() 在 Activity 从 Pause 状态转换到 Active 状态时被调用。在这个方法中访问SQLite数据库,通过Adapter将条目的内容和添加时间显示在ListView中。看代码:
package com.engineer.shizhibin.note;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {
   private Button mButton;
   private ListView mList;
   private Intent mIntent;
   private MyAdapter mAdapter;
   private NoteDb mNotedb;
   private Cursor cursor;
   private SQLiteDatabase dbreader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mList = (ListView) this.findViewById(R.id.list);
        mNotedb = new NoteDb(this);
        dbreader = mNotedb.getReadableDatabase();
        mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                cursor.moveToPosition(i);
                Intent intent = new Intent(MainActivity.this,ShowContent.class);
                intent.putExtra(NoteDb.ID,cursor.getInt(cursor.getColumnIndex(NoteDb.ID)));
                intent.putExtra(NoteDb.CONTENT,cursor.getString(cursor.getColumnIndex(NoteDb.CONTENT)));
                intent.putExtra(NoteDb.TIME,cursor.getString(cursor.getColumnIndex(NoteDb.TIME)));
                startActivity(intent);
            }
        });
    }

    public void add(View v) {
        mIntent = new Intent(MainActivity.this,AddContent.class);
        startActivity(mIntent);
    }
    public void selectDb() {
        cursor = dbreader.query
                (NoteDb.TABLE_NAME,null,null,null,null,null,null);
        mAdapter = new MyAdapter(this,cursor);
        mList.setAdapter(mAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectDb();
    }
}

    为了方便程序扩展,以后有可能添加图片、视频内容 我用了自定义的Adapter 继承自BadeAdapter:

package com.engineer.shizhibin.note;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private Cursor mCursor;
    private LinearLayout mLayout;
    public MyAdapter(Context mContext,Cursor mCursor) {
        this.mContext = mContext;
        this.mCursor = mCursor;
    }

    @Override
    public int getCount() {
        return mCursor.getCount();
    }

    @Override
    public Object getItem(int position) {
        return mCursor.getPosition();
    }

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

    @Override
    public View getView(int position, View view, ViewGroup viewGroup) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        mLayout = (LinearLayout) inflater.inflate(R.layout.item,null);
        TextView content = (TextView) mLayout.findViewById(R.id.list_content);
        TextView time = (TextView) mLayout.findViewById(R.id.list_time);
        mCursor.moveToPosition(position);
        String dbcontext = mCursor.getString(mCursor.getColumnIndex("content"));
        String dbtime = mCursor.getString(mCursor.getColumnIndex("time"));
        content.setText(dbcontext);
        time.setText(dbtime);
        return mLayout;
    }
}

添加条目,向数据中添加数据用ContentValues以键值对的方式 非常方便:

package com.engineer.shizhibin.note;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import java.text.SimpleDateFormat;
import java.util.Date;

public class AddContent extends AppCompatActivity {
    private EditText mEt;
    private NoteDb mDb;
    private SQLiteDatabase mSqldb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_content);
        mEt = (EditText) this.findViewById(R.id.text);
        mDb = new NoteDb(this);
        mSqldb = mDb.getWritableDatabase();
    }
    public void save(View v) {
        DbAdd();
        finish();
    }
    public void cancle(View v) {
        mEt.setText("");
        finish();
    }
    public void DbAdd() {
        ContentValues cv = new ContentValues();
        cv.put(NoteDb.CONTENT,mEt.getText().toString());
        cv.put(NoteDb.TIME,getTime());
        mSqldb.insert(NoteDb.TABLE_NAME,null,cv);
    }
    public String getTime() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = new Date();
        String str = sdf.format(date);
        return str;
    }
}

查看条目:

package com.engineer.shizhibin.note;

import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class ShowContent extends AppCompatActivity {
    private TextView mTextview;
    private TextView time;
    private NoteDb mDb;
    private SQLiteDatabase mSql;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_content);
        mTextview = (TextView)this.findViewById(R.id.showtext);
        time = (TextView)this.findViewById(R.id.showtime);
        mDb = new NoteDb(this);
        mSql = mDb.getWritableDatabase();
        mTextview.setText(getIntent().getStringExtra(NoteDb.CONTENT));
        time.setText(getIntent().getStringExtra(NoteDb.TIME));
    }
    public void delete(View v) {
        int id = getIntent().getIntExtra(NoteDb.ID,0);
        mSql.delete(NoteDb.TABLE_NAME," _id = " + id,null);
        finish();

    }
    public void goBack(View v) {
        finish();
    }
}

MainActivity布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/back"
    tools:context="com.engineer.shizhibin.note.MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="添加"
            android:onClick="add"/>
    </LinearLayout>
    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#000"
        android:layout_marginTop="20dp">

    </ListView>

</LinearLayout>

添加Activity布局:

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/back"
    tools:context="com.engineer.shizhibin.note.AddContent">
    <EditText
        android:id="@+id/text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="top"
        android:textColor="#000"
        android:hint="有了记事本,我再也不会忘记事情了!"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="保存"
            android:onClick="save"/>
        <Button
            android:id="@+id/cancle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="取消"
            android:onClick="cancle"/>
    </LinearLayout>
</LinearLayout>

查看页布局:

<?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:layout_width="match_parent"
    android:orientation="vertical"
    android:background="@drawable/back"
    android:layout_height="match_parent"
    tools:context="com.engineer.shizhibin.note.ShowContent">
    <TextView
        android:id="@+id/showtime"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:layout_marginLeft="10dp"
        android:textSize="20sp"/>
    <TextView
        android:id="@+id/showtext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:gravity="top"
        android:layout_weight="1"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:textColor="#000"
        android:hint="有了记事本,我再也不会忘记事情了!"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp">
        <Button
            android:id="@+id/delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="删除"
            android:onClick="delete"/>
        <Button
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="返回"
            android:onClick="goBack"/>
    </LinearLayout>
</LinearLayout>

ListView布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center_vertical"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/list_content"
            android:textColor="#000"
            android:textSize="20sp"
            android:text="tv"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/list_time"
            android:textColor="#000"
            android:textSize="20sp"
            android:text="time"/>

    </LinearLayout>
</LinearLayout>

以上就是所有源码,明白Android目录结构,复制到相应的地方,就可以运行了!!!

 

    

 

  • 161
    点赞
  • 861
    收藏
    觉得还不错? 一键收藏
  • 276
    评论
### 回答1: 记事本是一款简单实用的应用程序,适合轻度记事和便签。Android项目的开发案例记事本开发可以锻炼开发者的基本编程能力和UI设计能力,同时可以为新手了解Android应用程序开发提供一个较好的入门例子。以下是记事本应用程序源码开发步骤: 1. 创建一个Android项目,并在MainActivity类设计记事本的基本布局,包括工具栏、编辑框、滚动条和按钮等。 2. 在布局添加后台逻辑代码并实现多种功能,如文本保存、打开和编辑等。 3. 设计数据模型,包括有关文本的标题、内容和时间属性,并实现数据持久化,保存和读取。 4. 调整UI设计和交互,定义各种事件监听器和动作,例如单击、长按和滑动等,以及文本点击、选择和复制等。 5. 针对不同分辨率和设备,在布局和UI设计方面进行适配。此外,还应该考虑用户界面的可读性和可用性,并确保其符合Android的设计规范。 需要注意的是,在开发过程开发者应该编写清晰、简单、可重用且易于维护的代码,并尽可能利用相关类库和框架。同时,还应该进行良好的测试,确保应用程序的正确性和稳定性。最后,开发者可以在发布前对应用程序进行一些基本的检测和优化,以提高用户体验和应用市场的评价。 ### 回答2: 记事本是一款常见的应用程序,其功能包括文字输入、编辑、保存等。在Android平台上,记事本应用也是很多开发者入门的第一个示例项目。下面我会介绍一个简单记事本项目的开发过程及源码解析。 1. 项目结构 该记事本项目基于MVP模式开发,包含三个模块:View、Presenter和Model。View层负责界面显示和用户操作响应,Presenter层负责业务逻辑处理和数据传递,Model层负责数据的读写和持久化操作。 2. 功能实现 (1) 文本输入与显示:使用EditText控件实现用户可输入的文本框,使用TextView控件实现显示已保存的文本。 (2) 模板保存:应用程序预设多个模板,用户可在文本框选择模板并保存到手机本地。 (3) 文本保存和读取:用户输入的文本,可以保存到手机本地,也可以从本地读取已保存的文本。 (4) 删除:用户可选已经保存的文本,进行删除操作。 3. 项目核心代码 (1) Presenter层代码: Presenter层主要负责数据处理和业务逻辑的实现。其数据处理包括将用户输入的数据保存到本地,读取本地已保存的数据等。业务逻辑包括选择模板、删除已保存的数据等功能的实现。以下是Presenter层的核心代码: public class NotePresenter implements NoteContract.Presenter { private static final String FILE_NAME = "note.txt"; private NoteContract.View mView; public NotePresenter(NoteContract.View view) { mView = view; } @Override public void save(String text) { try { FileOutputStream fos = mView.getContext().openFileOutput(FILE_NAME, Context.MODE_APPEND); fos.write((text + "\n").getBytes()); fos.close(); mView.showSaveSuccess(); } catch (Exception e) { e.printStackTrace(); mView.showSaveFail(); } } @Override public void load() { try { FileInputStream fis = mView.getContext().openFileInput(FILE_NAME); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { sb.append(line); sb.append(System.getProperty("line.separator")); } br.close(); fis.close(); mView.showNoteList(sb.toString()); } catch (Exception e) { e.printStackTrace(); mView.showLoadFail(); } } @Override public void delete(String text) { try { FileInputStream fis = mView.getContext().openFileInput(FILE_NAME); BufferedReader br = new BufferedReader(new InputStreamReader(fis)); StringBuilder sb = new StringBuilder(); String line; while ((line = br.readLine()) != null) { if (!line.equals(text)) { sb.append(line); sb.append(System.getProperty("line.separator")); } } br.close(); fis.close(); FileOutputStream fos = mView.getContext().openFileOutput(FILE_NAME, Context.MODE_PRIVATE); fos.write(sb.toString().getBytes()); fos.close(); mView.showDeleteSuccess(); } catch (Exception e) { e.printStackTrace(); mView.showDeleteFail(); } } } (2) View层代码: View层主要负责界面的显示和用户交互的处理。在本项目,View层主要包括EditText和TextView两个控件的设置和具体功能实现。以下是View层的核心代码: public class NoteActivity extends AppCompatActivity implements NoteContract.View, View.OnClickListener { private static final String[] TEMPLATES = {"模板1", "模板2", "模板3", "模板4", "模板5"}; private Button mBtnSave, mBtnLoad, mBtnDelete, mBtnTemplate; private EditText mEtContent; private TextView mTvNoteList; private NoteContract.Presenter mPresenter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_note); mPresenter = new NotePresenter(this); initView(); } private void initView() { mBtnSave = findViewById(R.id.btn_save); mBtnLoad = findViewById(R.id.btn_load); mBtnDelete = findViewById(R.id.btn_delete); mBtnTemplate = findViewById(R.id.btn_template); mEtContent = findViewById(R.id.et_content); mTvNoteList = findViewById(R.id.tv_note_list); mBtnSave.setOnClickListener(this); mBtnLoad.setOnClickListener(this); mBtnDelete.setOnClickListener(this); mBtnTemplate.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_save: mPresenter.save(mEtContent.getText().toString()); break; case R.id.btn_load: mPresenter.load(); break; case R.id.btn_delete: mPresenter.delete(mTvNoteList.getText().toString()); break; case R.id.btn_template: showTemplateDialog(); break; } } private void showTemplateDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("选择模板"); builder.setItems(TEMPLATES, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String template = TEMPLATES[which]; mEtContent.setText(template); } }); builder.show(); } @Override public void showSaveSuccess() { Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show(); mEtContent.setText(""); } @Override public void showSaveFail() { Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show(); } @Override public void showNoteList(String noteList) { mTvNoteList.setText(noteList); } @Override public void showLoadFail() { Toast.makeText(this, "加载失败", Toast.LENGTH_SHORT).show(); } @Override public void showDeleteSuccess() { Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); } @Override public void showDeleteFail() { Toast.makeText(this, "删除失败", Toast.LENGTH_SHORT).show(); } @Override public Context getContext() { return this; } } 4. 总结 该记事本项目虽然比较简单,但是也包含了Android开发常见的基本功能实现。同时,该项目结合了MVP模式,使得项目代码结构清晰,易于维护。希望该项目对于刚入门Android开发的同学有所帮助。 ### 回答3: 记事本是一款很常用的应用程序,随着移动设备的普及,很多用户都喜欢使用记事本来记录日常的重要信息。因此,开发一款Android记事本应用程序是非常有必要和有意义的。 首先,Android记事本项目需要实现的功能就是基本的增删改查操作。用户可以新建便笺、编辑便笺、删除便笺或者查看便笺的详细信息等。除此之外,用户还可以设置闹钟提醒和备份数据等功能。 其次,该应用程序需要使用SQLite数据库来存储数据。SQLite是一种轻型的嵌入式关系型数据库系统,可以用来管理、存储和操作应用程序的数据。在该应用程序,我们需要通过SQLite数据库来创建和管理表格、插入、更新和删除数据等。 第三,为了提高用户体验,该应用程序需要具有良好的界面设计和交互体验。可以采用Material Design等设计规范,让应用程序看起来更加美观、直观。 最后,在项目开发过程,我们还需要考虑到代码的可读性和可维护性。因此,我们需要采用良好的编程规范和设计模式,将代码结构组织清晰,易于理解和维护。 总之,Android记事本项目的开发涉及到很多方面,无论是从功能还是从用户体验、代码质量方面来看,都需要进行全面考虑和细致实现。如果您有相关需求,不妨参考一下完整的Android记事本源码,可以为您的应用的开发提供很好的启示和参考。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 276
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值