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, );
}
@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, ), 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.後邊會上傳源碼,多多學習。