代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Java
】
记事本程序
作者:
千如
/ 发布于
2015/5/25
/
821
MainActivity package cn.dccssq; import android.app.ListActivity; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.SimpleCursorAdapter; public class MainActivity extends ListActivity { private static final int INSERT_ID = Menu.FIRST; private static final int DELETE_ID = Menu.FIRST + 1; private static final int ACTIVITY_CREATE = 0; private static final int ACTIVITY_EDIT = 1; private DiaryDbAdapter diaryDb; private Cursor cursor; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); diaryDb =new DiaryDbAdapter(this); diaryDb.open(); } private void showListView(){ cursor = diaryDb.getAllNotes(); String[] from = new String[]{DiaryDbAdapter.KEY_TITLE,DiaryDbAdapter.KEY_BODY}; int[] to = new int[]{R.id.text1,R.id.created}; ListAdapter cursorAdapter = new SimpleCursorAdapter(this,R.layout.diary_row,cursor,from,to); setListAdapter(cursorAdapter); } @Override protected void onListItemClick(ListView l, View v, int position, long id) { // TODO Auto-generated method stub super.onListItemClick(l, v, position, id); Cursor c = cursor; c.move(position); Intent intent = new Intent(this,ActivityDiary.class); intent.putExtra(DiaryDbAdapter.KEY_ROWID, id); intent.putExtra(DiaryDbAdapter.KEY_TITLE, c.getString(c .getColumnIndexOrThrow(DiaryDbAdapter.KEY_TITLE))); intent.putExtra(DiaryDbAdapter.KEY_BODY, c.getString(c .getColumnIndexOrThrow(DiaryDbAdapter.KEY_BODY))); startActivityForResult(intent, ACTIVITY_EDIT); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); showListView(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub super.onCreateOptionsMenu(menu); menu.add(0,INSERT_ID,0,R.string.menu_insert); menu.add(0,DELETE_ID,0,R.string.menu_delete); return true; } @Override public boolean onMenuItemSelected(int featureId, MenuItem item) { // TODO Auto-generated method stub switch(item.getItemId()){ case INSERT_ID: Log.i("INSERT:", String.valueOf(INSERT_ID)); createDiary(); return true; case DELETE_ID: Log.i("DELETE_ID:", String.valueOf(getListView().getSelectedItemId())); diaryDb.deleteDiary(getListView().getSelectedItemId()); showListView(); return true; } return super.onMenuItemSelected(featureId, item); } private void createDiary(){ Intent intent = new Intent(); intent.setClass(this, ActivityDiary.class); startActivityForResult(intent, ACTIVITY_CREATE); } } ActivityDiary package cn.dccssq; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; public class ActivityDiary extends Activity { // title EditText EditText titleTxt; // body EditText EditText bodyTxt; // save Button Button btn; // Row Id Long rowId; private DiaryDbAdapter diaryDb; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.notepad); // Initialize the DiaryDbAdapter. diaryDb = new DiaryDbAdapter(this); // Get the screen control titleTxt = (EditText)findViewById(R.id.title); bodyTxt = (EditText)findViewById(R.id.body_text); btn = (Button)findViewById(R.id.button); rowId = null ; // Get data from the front page Bundle bundle = getIntent().getExtras(); if(bundle!=null){ Log.i("bund:",bundle.toString()); // Set data to page String title = bundle.getString(DiaryDbAdapter.KEY_TITLE); String body = bundle.getString(DiaryDbAdapter.KEY_BODY); rowId = bundle.getLong(DiaryDbAdapter.KEY_ROWID); if(title!=null) { titleTxt.setText(title); } if(body!=null) { bodyTxt.setText(body); } } btn.setOnClickListener(new View.OnClickListener() { public void onClick(View arg0) { // TODO Auto-generated method stub String title = titleTxt.getText().toString(); String body = bodyTxt.getText().toString(); if(checkInput(title, body)){ diaryDb.open(); if(rowId!=null){ diaryDb.updateDiary(rowId, title, body); }else{ diaryDb.createDiary(title, body); } diaryDb.close(); Intent mIntent = new Intent(); setResult(RESULT_OK, mIntent); finish(); } } }); } /** * Validate the input. * @param title * @param body * @return */ public boolean checkInput(String title ,String body){ if(null==title || title.trim().length()==0){ titleTxt.setError("Please input the title!"); return false; } if(null==body || body.trim().length()==0){ bodyTxt.setError("Please input the content!"); return false; } return true; } } DiaryDbAdapter DB操作类,提供了两种增删查改的功能代码 package cn.dccssq; import java.util.Calendar; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; public class DiaryDbAdapter { public static final String KEY_TITLE = "title"; public static final String KEY_BODY = "body"; public static final String KEY_ROWID = "_id"; public static final String KEY_CREATED = "created"; private DatabaseHelper databaseHelper; private Context context; private SQLiteDatabase sqliteDatabase; public DiaryDbAdapter(Context context) { this.context = context; } /** * Open the Database */ public void open(){ databaseHelper = new DatabaseHelper(context); try { sqliteDatabase = databaseHelper.getWritableDatabase(); }catch(SQLiteException e){ sqliteDatabase = databaseHelper.getReadableDatabase(); } } /** * Close the Database */ public void close() { sqliteDatabase.close(); } /** * Insert the Data * @param title * @param body * @return */ public long createDiary(String title,String body){ ContentValues content = new ContentValues(); content.put(KEY_TITLE, title); content.put(KEY_BODY, body); Calendar calendar = Calendar.getInstance(); String created = calendar.get(Calendar.YEAR) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE); content.put(KEY_CREATED, created); return sqliteDatabase.insert(databaseHelper.DATABSE_TABLE, null, content); } /** * Delete the record * @param rowId * @return */ public boolean deleteDiary(long rowId){ String whereString = KEY_ROWID + "=" + rowId; return sqliteDatabase.delete(databaseHelper.DATABSE_TABLE, whereString, null)>0; } /** * Get all Records * @return */ public Cursor getAllNotes() { String[] searchResult = {KEY_ROWID, KEY_TITLE,KEY_BODY, KEY_CREATED}; return sqliteDatabase.query(databaseHelper.DATABSE_TABLE, searchResult, null, null, null, null, null); } /** * Get the record by condition * @param rowId * @return * @throws SQLException */ public Cursor getDiary(long rowId) throws SQLException{ String[] searchResult = {KEY_ROWID, KEY_TITLE,KEY_BODY, KEY_CREATED}; String whereString = KEY_ROWID + "=" + rowId; Cursor mCursor = sqliteDatabase.query(true, databaseHelper.DATABSE_TABLE, searchResult, whereString, null, null, null, null, null); if(mCursor!=null){ mCursor.moveToFirst(); } return mCursor; } public boolean updateDiary(long rowId ,String title,String body){ ContentValues values = new ContentValues(); values.put(KEY_ROWID, title); values.put(KEY_BODY,body); Calendar calendar = Calendar.getInstance(); String created = calendar.get(Calendar.YEAR) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE); values.put(KEY_CREATED, created); String whereString = KEY_ROWID + "=" + rowId; return sqliteDatabase.update(databaseHelper.DATABSE_TABLE, values, whereString, null)>0; } public void xinjianDiary(String title,String body){ Calendar calendar = Calendar.getInstance(); String created = calendar.get(Calendar.YEAR) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE); String insertSQL = "INSERT INTO " + databaseHelper.DATABSE_TABLE +"(" + KEY_ROWID +"," + KEY_TITLE+"," + KEY_BODY +"," + KEY_CREATED + ")" + " values (?,?,?,?)" ; Object[] args = {null,title,body,created}; sqliteDatabase.execSQL(insertSQL, args); } public void bianjiDiary(long rowId ,String title,String body){ Calendar calendar = Calendar.getInstance(); String created = calendar.get(Calendar.YEAR) + "/" + calendar.get(Calendar.MONTH) + "/" + calendar.get(Calendar.DAY_OF_MONTH) + " " + calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE); String updateSQL = "update " + databaseHelper.DATABSE_TABLE +" set " + KEY_TITLE+"=? ," + KEY_BODY +"=? ," + KEY_CREATED + "=? " + " where " + KEY_ROWID + "= ?" ; Object[] args = {title,body,created,rowId}; sqliteDatabase.execSQL(updateSQL, args); } public void shanchuDiary(long rowId ){ String deleteSQL = "delete from "+ databaseHelper.DATABSE_TABLE +" where " + KEY_ROWID + "= ?" ; Object[] args = {rowId}; sqliteDatabase.execSQL(deleteSQL, args); } public Cursor qudeAllNotes() { String searchSQL = "select _id , title , body ,created from "+ databaseHelper.DATABSE_TABLE; return sqliteDatabase.rawQuery(searchSQL, null); } } DatabaseHelper package cn.dccssq; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { private final static String DATABSE_NAME = "notepad"; private final static int DATABASE_VERSION = 1; public final static String DATABSE_TABLE = "diary"; private final static String DATABASE_CREATE = "create table " + DATABSE_TABLE + " (_id integer primary key autoincrement," + "title text not null, body text not null, created text not null);"; public DatabaseHelper(Context context) { super(context, DATABSE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DATABASE_CREATE); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { db.execSQL("DROP TABLE IF EXISTS " + DATABSE_TABLE); onCreate(db); } } main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:id="@+id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="您还没有开始写日记呢!点击下边的Menu按钮开始写日记吧:)" /> </LinearLayout> diary_row.xml <?xml version="1.0" encoding="utf-8" ?> <RelativeLayout android:id="@+id/row" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="30px" android:maxWidth="200dip" android:textSize="22sp" android:layout_marginTop="10dip" android:text="第一组第一项" /> <TextView android:id="@+id/created" android:layout_width="wrap_content" android:layout_height="35px" android:layout_alignParentRight="true" android:layout_marginLeft="10dip" android:layout_marginTop="10dip" android:text="1999年12月3号" /> </RelativeLayout> notepadxml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:text="@string/notepad_title" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/title" android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText> <TextView android:text="@string/notepad_body" android:layout_width="fill_parent" android:layout_height="wrap_content"></TextView> <EditText android:id="@+id/body_text" android:layout_width="fill_parent" android:layout_height="wrap_content"></EditText> <Button android:id="@+id/button" android:text="@string/notepad_button" android:layout_width="fill_parent" android:layout_height="wrap_content"></Button> </LinearLayout> strings.xml <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, MainActivity!</string> <string name="app_name">db2</string> <string name="diary_edit">编辑</string> <string name="notepad_title">标题:</string> <string name="notepad_body">内容:</string> <string name="notepad_button">确定:</string> <string name="menu_insert">Add</string> <string name="menu_delete">Delete</string> <string name="edit_diary">Edit</string> </resources> AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="cn.dccssq" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".ActivityDiary"> </activity> </application> <uses-sdk android:minSdkVersion="8" /> </manifest>
试试其它关键字
记事本
同语言下
.
List 切割成几份 工具类
.
一行一行读取txt的内容
.
Java PDF转换成图片并输出给前台展示
.
java 多线程框架
.
double类型如果小数点后为零则显示整数否则保留两位小
.
将图片转换为Base64字符串公共类抽取
.
sqlParser 处理SQL(增删改查) 替换schema 用于多租户
.
JAVA 月份中的第几周处理 1-7属于第一周 依次类推 29-
.
java计算两个经纬度之间的距离
.
输入时间参数计算年龄
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
千如
贡献的其它代码
(
9
)
.
Android发送接收短信
.
Android打电话
.
用户注册的Activity
.
动态添加删除Spinner
.
记事本程序
.
取得已安装程序列表
.
访问webservice
.
html5中页面拨打电话的方式
.
如何获wifi路由器的BSSID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3