现在就差最后一个界面,编辑界面的实现了,编辑界面采用LinearLayout布局,最上面是四个单选按钮,表示四种不同的事件类型,然后再是一个EditText文本编辑框,在之后就是两个Button按钮,addcontent.xml的代码如下:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.gionee.Note.MainActivity"
>
<RadioGroup
android:id="@+id/rp"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#ffff00" >
<RadioButton
android:id="@+id/memo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:checked="true"
android:text="备忘"
/>
<RadioButton
android:id="@+id/meeting"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="会议"
/>
<RadioButton
android:id="@+id/anni"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="纪念"
/>
<RadioButton
android:id="@+id/waitdo"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="待办" />
</RadioGroup>
<EditText
android:background="#ADFF2F"
android:id="@+id/ettext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dip"
android:paddingTop="16dip"
android:layout_weight="1"
android:gravity="top"
android:hint="有了记事本,我再也不会忘记事情了"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dip" >
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="保存" />
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
</LinearLayout>
</LinearLayout>
之后就是编辑界面,在这里实现了记事本编辑界面的按钮功能实现,分别是四种类型的单选按钮,保存和取消按钮其中modifyNote()则是通过判断.实现java代码如下:
package com.example.jishiben;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
public class EditNoteActivity extends Activity {
private RadioGroup rgType;
private Button savebtn, cancelbtn;
private EditText ettext;
private NoteManager noteManager;
private Note note;
private String type = "备忘";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontent);
Intent intent = getIntent();
try {
note = (Note) intent.getSerializableExtra("note");
} catch (Exception e) {
}
noteManager = new NoteManager(this);
savebtn = (Button) findViewById(R.id.save);
cancelbtn = (Button) findViewById(R.id.cancel);
ettext = (EditText) findViewById(R.id.ettext);
rgType = (RadioGroup) findViewById(R.id.rp);
if(note != null) {
ettext.setText(note.getIntroduction());
}
savebtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(note != null) {
modifyNote();
} else {
saveNote();
}
finish();
}
});
cancelbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
rgType.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.memo) {
type = "备忘";
} else if(checkedId == R.id.meeting) {
type = "会议";
} else if(checkedId == R.id.anni) {
type = "纪念";
} else {
type = "待办";
}
}
});
}
private void saveNote() {
String intr = ettext.getText().toString();
if(intr == null || "".equals(intr.trim())) return;
Note note = new Note();
note.setIntroduction(intr);
String date = DateUtil.formatDate(System.currentTimeMillis());
note.setDate(date);
note.setType(type);
boolean success = noteManager.save(note);
if(success) {
Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "保存失败", Toast.LENGTH_SHORT).show();
}
}
private void modifyNote() {
String intr = ettext.getText().toString();
if(intr == null || "".equals(intr.trim())) {
noteManager.deleteNote(note);
return;
}
note.setIntroduction(intr);
String date = DateUtil.formatDate(System.currentTimeMillis());
note.setDate(date);
note.setType("备忘");
boolean success = noteManager.modifyNote(note);
if(success) {
Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "修改失败", Toast.LENGTH_SHORT).show();
}
}
}
最后还有一个记事本事件的数据库操作类,NoteManager.java代码如下:
package com.example.jishiben;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class NoteManager {
private Context mContext;
public NoteManager(Context context) {
super();
this.mContext = context;
}
public boolean save(Note note) {
if(note == null) return false;
DBHelper dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("date", note.getDate());
values.put("type", note.getType());
values.put("introduction", note.getIntroduction());
boolean success = false;
try {
db.beginTransaction();
db.insert(DBHelper.TABLE_NAME, null, values);
db.setTransactionSuccessful();
success = true;
} catch (Exception e) {
e.printStackTrace();
success = false;
} finally {
db.endTransaction();
db.close();
dbHelper.close();
}
return success;
}
public boolean deleteNote(Note note) {
if(note == null) return false;
DBHelper dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
int res = 0;
try {
res = db.delete(DBHelper.TABLE_NAME, "id=?", new String[] {String.valueOf(note.getId())});
} catch (Exception e) {
e.printStackTrace();
} finally {
db.close();
dbHelper.close();
}
return res > 0;
}
public List<Note> getNotes(int pageNo, int pageSize) {
List<Note> notes = new ArrayList<Note>();
DBHelper dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query(false, DBHelper.TABLE_NAME,
new String[] {"id", "date", "type", "introduction"},
null, null, null, null, "date desc",
(pageNo - 1) * pageSize + "," + pageSize);
if(cursor != null) {
try {
while(cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String type = cursor.getString(cursor.getColumnIndex("type"));
String date = cursor.getString(cursor.getColumnIndex("date"));
String intr = cursor.getString(cursor.getColumnIndex("introduction"));
Note note = new Note();
note.setDate(date);
note.setIntroduction(intr);
note.setId(id);
note.setType(type);
notes.add(note);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
cursor.close();
db.close();
dbHelper.close();
}
}
return notes;
}
public boolean modifyNote(Note note) {
if(note == null) return false;
DBHelper dbHelper = new DBHelper(mContext);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("type", note.getType());
values.put("date", note.getDate());
values.put("introduction", note.getIntroduction());
int res = 0;
try {
res = db.update(DBHelper.TABLE_NAME, values, "id=?",
new String[] {String.valueOf(note.getId())});
} catch (Exception e) {
e.printStackTrace();
} finally {
db.close();
dbHelper.close();
}
return res > 0;
}
}
好了!我的记事本的基本功能算是完成了,接下来就是美化界面了.
附:记事本的要求
记事本,按照如下要求完成:
a) 基本功能:
i. 登陆后列表显示已存在的记事本。列表item要求:第一行显示内容简介,第二行显示最新编辑时间和类型。
ii. 类型分四类,会议,备忘,纪念日和待办事项(在系统里找四个icon表示吧)
iii. menu实现新建和全部删除功能,全部删除要有对话框确认,删除时提示删除进度。
iv. 新建可以选择分类,4类即可(如上)。
v. 长按item弹出上下文菜单,有编辑、删除和转发(调用短信)。
vi. 可在桌面添加widget,分类型显示记事本数目吧(可选)
b) 要求:
i. 不同主题下显示ok。
ii. 适应不同分辨率设备。