内容发布更新时间 : 2024/11/15 15:42:18星期一 下面是文章的全部内容请认真阅读。
5.2 编辑内容
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);//
setTheme(Android.R.style.Theme_Black); final Intent intent = getIntent();
final String action = intent.getAction(); setContentView(R.layout.diary_edit);
mTitleText = (EditText) findViewById(R.id.title); mBodyText = (EditText) findViewById(R.id.body); //确定按钮
confirmButton = (Button) findViewById(R.id.confirm);
if (EDIT_DIARY_ACTION.equals(action)) {// 编辑记事本 mState = STATE_EDIT; mUri = intent.getData();
mCursor = managedQuery(mUri, PROJECTION, null, null, null); mCursor.moveToFirst();
String title = mCursor.getString(1); mTitleText.setTextKeepState(title); String body = mCursor.getString(2); mBodyText.setTextKeepState(body);
setResult(RESULT_OK, (new Intent()).setAction(mUri.toString())); setTitle(\编辑记事\
} else if (INSERT_DIARY_ACTION.equals(action)) {// 新建日记 mState = STATE_INSERT; setTitle(\新建记事\} else {
Log.e(TAG, \finish(); r
getContentResolver().insert(Diary.DiaryColumns.CONTENT_URI, values); private void updateDiary() { String title = mTitleText.getText().toString(); String body = mBodyText.getText().toString(); ContentValues values = new ContentValues(); values.put(Diary.DiaryColumns.CREATED, DiaryContentProvider .getFormateCreatedDate()); values.put(Diary.DiaryColumns.TITLE, title); values.put(Diary.DiaryColumns.BODY, body); getContentResolver().update(mUri, values, null, null); } }
5.3 删除记事
private void renderListView() { Cursor cursor = managedQuery(getIntent().getData(), PROJECTION, null, null, DiaryColumns.DEFAULT_SORT_ORDER); SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.diary_row, cursor, new String[] { DiaryColumns.TITLE, DiaryColumns.CREATED }, new int[] { R.id.text1, R.id.created }); setListAdapter(adapter); }
5.4 程序执行流程
MainActivity.java文件:
package com.zxl.Ex03_07;
import com.zxl.service.FileService; import Android.app.Activity; import Android.os.Bundle; import Android.view.View; import Android.widget.Button; import Android.widget.EditText; import Android.widget.Toast;
public class MainActivity extends Activity { EditText editText1=null; EditText editText2=null; Button button1=null; FileService service=null;
/** Called when the activity is first created. */ @Override
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);
editText1=(EditText)findViewById(R.id.edit1); editText2=(EditText)findViewById(R.id.edit2); button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String filename=editText1.getText().toString()+\String content=editText2.getText().toString(); service=new FileService(MainActivity.this); try
{
service.save(filename, content); } catch (Exception e) {
e.printStackTrace(); }finally{
Toast.makeText(MainActivity.this,
getString(R.string.sucess),
Toast.LENGTH_SHORT).show();
} } }); } }
用于文件操作的类:FileService.java package com.zxl.service;
import java.io.FileOutputStream; import Android.content.Context; public class FileService { Context context;
public FileService(Context context){ this.context=context; }
public void save(String filename,String content)throws Exception{ FileOutputStream
outputStream=context.openFileOutput(filename,Context.MODE_PRIVATE); outputStream.write(content.getBytes()); outputStream.close(); } }
布局文件:main.xml:
Android:layout_width=\Android:layout_height=\ Android:background=\> Android:id=\ Android:layout_width=\Android:layout_height=\ Android:text=\Android:textSize=\ Android:layout_gravity=\ Android:paddingTop=\/> Android:id=\ Android:layout_width=\Android:layout_height=\Android:textSize=\ Android:text=\Android:paddingTop=\Android:textColor=\ /> Android:id=\ Android:layout_width=\Android:layout_height=\Android:maxLines=\ /> Android:id=\ Android:layout_width=\Android:layout_height=\Android:text=\Android:textSize=\ Android:textColor=\/> Android:id=\ Android:layout_width=\Android:layout_height=\Android:minLines=\ />
6 程序截图 6.1 添加新记事
5-2 编辑记事