GPhone--Android学习笔记(三):练习(2) Notepad V2

本篇在在Notepad V1的基础上完善下面功能:
1.新增删除功能
2.新增编辑功能
3.学习编写新的类NoteEdit.class
4.学习使用Intent

Step 1. 在strings.xml中新增一个<string name="menu_delete">Delete Item</string>

Step 2.在Notepadv1.java中实现Delete菜单

   增加代码如下:
   public static final int DELETE_ID = Menu.FIRST+1;//新增


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, INSERT_ID, R.string.menu_insert);
        menu.add(1, DELETE_ID, R.string.menu_delete);//新增
        return result;
    }


    @Override
    public boolean onOptionsItemSelected(Item item) {
     
     List<Row> rows = dbHelper.fetchAllRows();
     
        switch (item.getId()) {
        case INSERT_ID:
            createNote();
            fillData();
            break;
        case DELETE_ID:
            dbHelper.deleteRow(rows.get(getSelection()).rowId);//新增
            fillData();
       break;
        }

Step 3. 将createNote()改成如下,NoteEdit.class将在后面实现.
 这里主要是启动编辑界面: 
  Intent i = new Intent(this, NoteEdit.class);
        startSubActivity(i, ACTIVITY_CREATE);


Step 4. 实现点击某一项进入编辑界面:

    新增下面代码: 
    private static final int ACTIVITY_CREATE=0;
   
    private static final int ACTIVITY_EDIT=1;
   
    public static final String KEY_ROW_ID = "rowid";


    重载ItemClick函数
    @Override
    public void onListItemClick(ListView l,View v, int position, long id )
    {
     List<Row> rows = dbHelper.fetchAllRows();
     
        super.onListItemClick(l, v, position, id);
        Intent i = new Intent(this, NoteEdit.class);
        i.putExtra(KEY_ROW_ID, rows.get(position).rowId);
        i.putExtra(KEY_BODY, rows.get(position).body);
        i.putExtra(KEY_TITLE, rows.get(position).title);
        startSubActivity(i, ACTIVITY_EDIT);
    }

    重载获取编辑界面的返回值函数:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, String data, Bundle extras) {
     super.onActivityResult(requestCode, resultCode, data, extras);
       
        switch(requestCode) {
        case ACTIVITY_CREATE:
            String title = extras.getString(KEY_TITLE);
            String body = extras.getString(KEY_BODY);
            dbHelper.createRow(title, body);
            fillData();
            break;
        case ACTIVITY_EDIT:
            Long rowId = extras.getLong(KEY_ROW_ID);
            if (rowId != null) {
                String editTitle = extras.getString(KEY_TITLE);
                String editBody = extras.getString(KEY_BODY);
                dbHelper.updateRow(rowId, editTitle, editBody);
            }
            fillData();
            break;
        }
    }


5.新建文件notes_edit.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">
 
 <LinearLayout android:orientation="horizontal"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content">

  <TextView android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="@string/title" />
  <EditText id="@+id/title"
    android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_weight="1"/>
 </LinearLayout>

 <TextView android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="@string/body" />
 <EditText id="@+id/body" android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:scrollbars="vertical" />
 
 <Button id="@+id/confirm"
   android:text="@string/confirm"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" />

</LinearLayout>


    <string name="body">Body</string>
    <string name="title">Title</string>
    <string name="confirm">Confirm</string>

6.新建类NoteEdit,并从android.app.Activity这里继承,并重载onCreate(Bundle)(操作:右键点击Class,然后选择Source-

>Override/Implement Methods... )

实现的内容如下:
package com.google.android.demo.notepad1;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class NoteEdit extends Activity {

 private EditText titleText;
 private EditText bodyText;
 private Long rowId;
 
        //做初始化
 @Override
 protected void onCreate(Bundle icicle) {
  super.onCreate(icicle);
        setContentView(R.layout.notes_edit);
       
        titleText = (EditText) findViewById(R.id.title);
        bodyText = (EditText) findViewById(R.id.body);
      
        Button confirmButton = (Button) findViewById(R.id.confirm);
       
        rowId = null;
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String title = extras.getString(Notepadv1.KEY_TITLE);
            String body = extras.getString(Notepadv1.KEY_BODY);
            rowId = extras.getLong(Notepadv1.KEY_ROW_ID);
          
            if (title != null) {
                titleText.setText(title);
            }
            if (body != null) {
                bodyText.setText(body);
            }
        }
        //处理确认按钮
        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
             Bundle bundle = new Bundle();
               
                bundle.putString(Notepadv1.KEY_TITLE, titleText.getText().toString());
                bundle.putString(Notepadv1.KEY_BODY, bodyText.getText().toString());
                if (rowId != null) {
                    bundle.putLong(Notepadv1.KEY_ROW_ID, rowId);
                }
               
                setResult(RESULT_OK, null, bundle);
                finish();
            }
          
        });
 }

}


7.在AndroidManifest.xml中加入<activity class=".NoteEdit"/>

8.运行

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值