android简单记事本
来源:互联网 发布:car软件 编辑:程序博客网 时间:2024/06/06 04:39
android简单记事本
近段学习数据存储写个小程序,简单记事本,实现点击查看删除,添加保存。闲话不多说直接步入正题。
一、上图看着更直观。
1、这是该程序的主界面。上边是一个Button,下边是ListView显示记录的笔记
2、 点击添加按钮显示的界面,非常简单,输入,保存,取消
3、 这是点击主界面笔记后的显示,笔记,删除,返回
二、下边开始实现阶段:
1》先写布局文件
1、主布局文件activity_main.xml
一个添加按钮,一个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" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/main_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加"/> </LinearLayout> <ListView android:id="@+id/main_list" android:layout_width="match_parent" android:layout_height="wrap_content"/></LinearLayout>
2、点击添加之后的跳转界面add_list.xml
上边是一个EditText,下边是两个按钮保存和删除
<?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" android:orientation="vertical"> <EditText android:id="@+id/add_etv" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:background="@null" android:gravity="top" android:hint="记 录 点 滴,改 变 生 活" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/add_save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存" /> <Button android:id="@+id/add_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="80dp" android:text="取消" /> </LinearLayout></LinearLayout>
3、点击主界面的笔记跳转的界面,进行查看或者删除。select.xml
<?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" android:orientation="vertical"> <TextView android:id="@+id/s_tv" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:id="@+id/s_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="删除" /> <Button android:id="@+id/s_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="返回" /> </LinearLayout></LinearLayout>
4、记录笔记显示在主界面的布局文件。list.xml
<?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="fill_parent" android:layout_height="100dp" android:gravity="center_vertical" android:orientation="vertical"> <TextView android:id="@+id/list_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tv" /> <TextView android:id="@+id/list_time" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tv" /> </LinearLayout></LinearLayout>
2》不同功能的类文件
1、建NotesDB.class文件用来建表写入数据类型
package com.example.guo.my_account;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class NotesDB 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 NotesDB(Context context) { super(context, "notes", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table " + TABLE_NAME + " (" + ID + " integer primary key autoincrement," + CONTENT + " text not null," + TIME + " text not null)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}
2、建立自定义数据文件MyAdapter.class
package com.example.guo.my_account;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 context; private Cursor cursor; private LinearLayout layout; public MyAdapter(MainActivity context, Cursor cursor){ this.context = context; this.cursor = cursor; } @Override public int getCount() { return cursor.getCount(); } @Override public Object getItem(int position) { return cursor.getPosition(); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { LayoutInflater inflater = LayoutInflater.from(context); layout = (LinearLayout) inflater.inflate(R.layout.list,null); TextView contenttv = (TextView) layout.findViewById(R.id.list_content); TextView timetv = (TextView) layout.findViewById(R.id.list_time); cursor.moveToPosition(position); String content = cursor.getString(cursor.getColumnIndex("content")); String time = cursor.getString(cursor.getColumnIndex("time")); contenttv.setText(content); timetv.setText(time); return layout; }}
3、建立添加数据文件Addcontent.class
package com.example.guo.my_account;import android.app.Activity;import android.content.ContentValues;import android.content.Intent;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import java.util.Date;import java.text.SimpleDateFormat;public class Addcontent extends Activity implements View.OnClickListener { private Button savebtn, deletebtn; private EditText ettext; private NotesDB notesDB; private SQLiteDatabase dbWriter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_list); savebtn = (Button) findViewById(R.id.add_save); deletebtn = (Button) findViewById(R.id.add_delete); ettext = (EditText) findViewById(R.id.add_etv); savebtn.setOnClickListener(this); deletebtn.setOnClickListener(this); notesDB = new NotesDB(this); dbWriter = notesDB.getWritableDatabase(); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.add_save: addDB(); finish(); break; case R.id.add_delete: finish(); break; } } private void addDB() { ContentValues cv = new ContentValues(); cv.put(NotesDB.CONTENT, ettext.getText().toString()); cv.put(NotesDB.TIME, getTime()); dbWriter.insert(NotesDB.TABLE_NAME, null, cv); } public String getTime() { SimpleDateFormat format = new SimpleDateFormat("yyy年MM月dd日 HH:mm:ss"); Date curDate = new Date(); String str = format.format(curDate); return str; }}
4、建立修改删除文件Select.class
package com.example.guo.my_account;import android.app.Activity;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.TextView;public class SelectAct extends Activity implements View.OnClickListener { private Button s_delete, s_back; private TextView s_tv; private NotesDB notesDB; private SQLiteDatabase dbWriter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.select); s_back = (Button) findViewById(R.id.s_back); s_delete = (Button) findViewById(R.id.s_delete); s_tv = (TextView) findViewById(R.id.s_tv); notesDB = new NotesDB(this); dbWriter = notesDB.getWritableDatabase(); s_back.setOnClickListener(this); s_delete.setOnClickListener(this); s_tv.setText(getIntent().getStringExtra(NotesDB.CONTENT)); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.s_delete: deleteDate(); finish(); break; case R.id.s_back: finish(); break; } } private void deleteDate() { dbWriter.delete(NotesDB.TABLE_NAME, "_id=" + getIntent().getIntExtra(NotesDB.ID, 0), null); }}
5、MainActivity.class
package com.example.guo.my_account;import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.Button;import android.widget.ListView;import java.util.Timer;public class MainActivity extends Activity implements View.OnClickListener { private Button addbtn; private ListView lv; private Intent i; private SQLiteDatabase dbReader; private Cursor cursor; private NotesDB notesDB; private MyAdapter adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { addbtn = (Button) findViewById(R.id.main_add); lv = (ListView) findViewById(R.id.main_list); addbtn.setOnClickListener(this); notesDB = new NotesDB(this); dbReader = notesDB.getReadableDatabase(); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { cursor.moveToPosition(position); Intent i = new Intent(MainActivity.this, SelectAct.class); i.putExtra(NotesDB.ID, cursor.getInt(cursor.getColumnIndex(NotesDB.ID))); i.putExtra(notesDB.CONTENT, cursor.getString(cursor.getColumnIndex(NotesDB.CONTENT))); i.putExtra(notesDB.TIME, cursor.getString(cursor.getColumnIndex(NotesDB.TIME))); startActivity(i); } }); } @Override public void onClick(View v) { i = new Intent(this, Addcontent.class); startActivity(i); } private void selectDB() { cursor = dbReader.query(NotesDB.TABLE_NAME, null, null, null, null, null, null); adapter = new MyAdapter(this, cursor); lv.setAdapter(adapter); } @Override protected void onResume() { super.onResume(); selectDB(); }}
3》不要忘了在AndroidManifest.xml文件中声明
代码如下
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.guo.my_account"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@android:style/Theme.Light.NoTitleBar"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Addcontent"/> <activity android:name=".SelectAct"/> </application></manifest>
自己写的,不是很简单实用,有声明不足的希望留言,会改进。Thanks.后边会上传源码,多多学习。
2 0
- Android 简单的记事本
- android简单记事本
- android 简单记事本开发(一)
- 简单记事本
- android 记事本
- 自己写的一个简单的android记事本app
- 一个简单的记事本
- 简单的记事本
- 简单的Java记事本
- java开发简单记事本
- 一个简单的记事本
- 一个简单的记事本
- Qt实现简单记事本
- Java简单记事本实现
- java简单记事本
- 简单记事本开发
- java简单的记事本
- 简单记事本程序
- leetcode-22 Merge k Sorted Lists
- Linux中断研究
- js checkBox,Radio选中
- Cookie总结
- 我对sso的使用和实现
- android简单记事本
- leetcode-23 Insertion Sort List
- 单点登录
- 静态库的使用方法
- 欢迎使用CSDN-markdown编辑器
- MySQL中group_concat函数
- 使用VS2008自带的dumpbin.exe查看dll包含的函数
- spring DataSourceUtils
- lightoj 1060 - nth Permutation 组合数学