国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android——記事本功能業(yè)務(wù)(完整代碼)

這篇具有很好參考價值的文章主要介紹了Android——記事本功能業(yè)務(wù)(完整代碼)。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

目錄

實(shí)現(xiàn)效果

一、搭建記事本頁面布局activity_notepad.xml

二、搭建記事本界面Item布局notepad_item_layout.xml

三、封裝記錄信息實(shí)體類NotepadBean類

四、編寫記事本界面列表適配器NotepadAdapter類

五、創(chuàng)建數(shù)據(jù)庫

六、實(shí)現(xiàn)記事本界面的顯示功能NotepadAdapter.java?

七、搭建添加記錄界面和修改記錄界面的布局activity_record.xml?

八、實(shí)現(xiàn)添加記錄界面的功能RecordActivity.java?

九、實(shí)現(xiàn)修改記錄界面的功能?

十、刪除記事本中的記錄?


實(shí)現(xiàn)效果

android記事本app代碼,AndroidStudio,android,android studioandroid記事本app代碼,AndroidStudio,android,android studioandroid記事本app代碼,AndroidStudio,android,android studioandroid記事本app代碼,AndroidStudio,android,android studioandroid記事本app代碼,AndroidStudio,android,android studio

一、搭建記事本頁面布局activity_notepad.xml

1.創(chuàng)建項(xiàng)目

項(xiàng)目名為Notepad,指定包名為cn.itcast.notepad

Activity名稱為NotepadActivity,布局文件名為activity_notepad

2.導(dǎo)入圖片到res文件夾下面 一個新建的文件夾drawable-hdpi中

android記事本app代碼,AndroidStudio,android,android studio

3.頁面代碼如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fefefe">
    <TextView
        android:id="@+id/note_name"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:textSize="20sp"
        android:textColor="@android:color/white"
        android:gravity="center"
        android:textStyle="bold"
        android:background="#fb7a6a"
        android:text="記事本"/>
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:divider="#E4E4E4"
        android:dividerHeight="1dp"
        android:fadingEdge="none"
        android:listSelector="#00000000"
        android:scrollbars="none"
        android:layout_below="@+id/note_name">
    </ListView>
    <ImageView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/add"
        android:layout_marginBottom="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

4.修改清單文件

項(xiàng)目創(chuàng)建后所有界面最上方都有一個綠色的默認(rèn)標(biāo)題欄,不美觀。在AndroidManifest.xml修改代碼去掉標(biāo)題欄。 在<application>標(biāo)簽中修改為:

android:theme="@style/Theme.AppCompat.NoActionBar"

二、搭建記事本界面Item布局notepad_item_layout.xml

由于記事本界面使用了ListView控件展示記錄列表,因此需要創(chuàng)建一個該列表的Item界面。

1.創(chuàng)建布局文件

res/layout文件夾中,右擊【NEW】-【XML】-【Layout XML File】,名字為notepad_item_layout

2.布局notepad_item_layout.xml界面

放置兩個TextView控件,分別用于顯示記錄的部分內(nèi)容與保存的時間。

代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fefefe">
    <TextView
        android:id="@+id/note_name"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:textSize="20sp"
        android:textColor="@android:color/white"
        android:gravity="center"
        android:textStyle="bold"
        android:background="#fb7a6a"
        android:text="記事本"/>
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:divider="#E4E4E4"
        android:dividerHeight="1dp"
        android:fadingEdge="none"
        android:listSelector="#00000000"
        android:scrollbars="none"
        android:layout_below="@+id/note_name">
    </ListView>
    <ImageView
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/add"
        android:layout_marginBottom="30dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

三、封裝記錄信息實(shí)體類NotepadBean類

創(chuàng)建一個NotepadBean類,存放每個記錄的記錄內(nèi)容和保存時間屬性。

1.選中cn.itcast.notepad包,右擊-【New】-【Package】,命名為bean,創(chuàng)建一個bean包。

在該包中,右擊-【New】-【JavaClass】,命名為NotepadBean。

android記事本app代碼,AndroidStudio,android,android studio

2.NotepadBean.java代碼如下

package cn.itcast.notepad.bean;

public class NotepadBean {
    private String id;                  //記錄的id
    private String notepadContent;   //記錄的內(nèi)容
    private String notepadTime;       //保存記錄的時間
 //右擊-【Generate】-【Getter and Setter】,產(chǎn)生下面代碼
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getNotepadContent() {
        return notepadContent;
    }
    public void setNotepadContent(String notepadContent) {
        this.notepadContent = notepadContent;
    }
    public String getNotepadTime() {
        return notepadTime;
    }
    public void setNotepadTime(String notepadTime) {
        this.notepadTime = notepadTime;
    }
}

四、編寫記事本界面列表適配器NotepadAdapter類

創(chuàng)建數(shù)據(jù)適配器NotepadAdapter對ListView控件進(jìn)行數(shù)據(jù)適配。

1.選中cn.itcast.notepad包,右擊-【New】-【Package】,命名為adapter。

在該包中,右擊-【New】-【JavaClass】,命名為NotepadAdapter。

android記事本app代碼,AndroidStudio,android,android studio

?2.(1)NotepadAdapter類繼承自BaseAdapter類,然后點(diǎn)擊【Alt】+【Enter】,【implement methods】重寫方法getCount()、getItem()、 getItemId()、getView()。

(2)創(chuàng)建構(gòu)造方法

public NotepadAdapter(Context context, List<NotepadBean> list)

通過inflate()方法加載Item界面的布局文件。

private LayoutInflater layoutInflater;
    private List<NotepadBean> list;
    public NotepadAdapter(Context context, List<NotepadBean> list){
        this.layoutInflater=LayoutInflater.from(context);
        this.list=list;
    }

(3)創(chuàng)建ViewHolder類

在類里面創(chuàng)建構(gòu)造方法,在該方法中初始化Item界面(即notepad_item_layout.xml)上的控件。

public ViewHolder(View view){
            tvNoteoadContent=(TextView) view.findViewById(R.id.item_content);
            tvNotepadTime=(TextView) view.findViewById(R.id.item_time);
        }

接著繼續(xù)在getView方法里面寫代碼?

3.代碼如下:

package cn.itcast.notepad.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.itcast.notepad.R;
import cn.itcast.notepad.bean.NotepadBean;

public class NotepadAdapter extends BaseAdapter{
    private LayoutInflater layoutInflater;
    private List<NotepadBean> list;
    public NotepadAdapter(Context context, List<NotepadBean> list){
        this.layoutInflater=LayoutInflater.from(context);
        this.list=list;
    }
    @Override
    public int getCount() {
        return list==null ? 0 : list.size();
    }
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
            convertView=layoutInflater.inflate(R.layout.notepad_item_layout,null);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder) convertView.getTag();
        }
        NotepadBean noteInfo=(NotepadBean) getItem(position);
        viewHolder.tvNoteoadContent.setText(noteInfo.getNotepadContent());
        viewHolder.tvNotepadTime.setText(noteInfo.getNotepadTime());
        return convertView;
    }
    class ViewHolder{
        TextView tvNoteoadContent;;
        TextView tvNotepadTime;
        public ViewHolder(View view){
            tvNoteoadContent=(TextView) view.findViewById(R.id.item_content);
            tvNotepadTime=(TextView) view.findViewById(R.id.item_time);
        }
    }
}

五、創(chuàng)建數(shù)據(jù)庫

在這個記事本程序中存儲和讀取記錄的數(shù)據(jù)都是通過操作數(shù)據(jù)庫完成的,因此需要創(chuàng)建數(shù)據(jù)庫類SQLiteHelper和數(shù)據(jù)庫的工具類DBUtils.java。

(一)創(chuàng)建DBUtils類

在該類中定義數(shù)據(jù)庫的名稱、表名、數(shù)據(jù)庫版本、數(shù)據(jù)庫表中的列名以及獲取當(dāng)前日期等信息。

1.選中cn.itcast.notepad包,右擊-【New】-【Package】,命名為utils。

在該包中,右擊-【New】-【JavaClass】,命名為DBUtils。

android記事本app代碼,AndroidStudio,android,android studio

?2.DBUtils.java代碼如下

package cn.itcast.notepad.utils;

import java.text.SimpleDateFormat;
import java.util.Date;
public class DBUtils {
    public static final String DATABASE_NAME = "Notepad";//數(shù)據(jù)庫名
    public static final String DATABASE_TABLE = "Note";  //表名
    public static final int DATABASE_VERION = 1;          //數(shù)據(jù)庫版本
    //數(shù)據(jù)庫表中的列名
    public static final String NOTEPAD_ID = "id";
    public static final String NOTEPAD_CONTENT = "content";
    public static final String NOTEPAD_TIME = "notetime";
    //獲取當(dāng)前日期
    public static final String getTime(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);
    }
}

(二)創(chuàng)建SQLiteHelper類

創(chuàng)建Notepad的數(shù)據(jù)庫,實(shí)現(xiàn)增刪改查的功能。

1.選中cn.itcast.notepad包,右擊-【New】-【Package】,命名為database。

在該包中,右擊-【New】-【JavaClass】,命名為SQLiteHelper。

android記事本app代碼,AndroidStudio,android,android studio

2.SQLiteHelper.java代碼如下

package cn.itcast.notepad.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

import cn.itcast.notepad.bean.NotepadBean;
import cn.itcast.notepad.utils.DBUtils;

public class SQLiteHelper extends SQLiteOpenHelper {
    private SQLiteDatabase sqLiteDatabase;
    //創(chuàng)建數(shù)據(jù)庫
    public SQLiteHelper(Context context){
        super(context, DBUtils.DATABASE_NAME, null, DBUtils.DATABASE_VERION);//調(diào)用了DBUtils類,得到數(shù)據(jù)庫名Notepad
        sqLiteDatabase = this.getWritableDatabase();
    }
    //創(chuàng)建表,用execSQL()方法創(chuàng)建一個數(shù)據(jù)表,列名分別為ID、CONTENT、TIME
    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table "+DBUtils.DATABASE_TABLE+"("+DBUtils.NOTEPAD_ID+
                " integer primary key autoincrement,"+ DBUtils.NOTEPAD_CONTENT +
                " text," + DBUtils.NOTEPAD_TIME+ " text)");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
    //添加數(shù)據(jù)
    public boolean insertData(String userContent,String userTime){
        ContentValues contentValues=new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT,userContent);
        contentValues.put(DBUtils.NOTEPAD_TIME,userTime);
        return
                sqLiteDatabase.insert(DBUtils.DATABASE_TABLE,null,contentValues)>0;
    }
    //刪除數(shù)據(jù)
    public boolean deleteData(String id){
        String sql=DBUtils.NOTEPAD_ID+"=?";
        String[] contentValuesArray=new String[]{String.valueOf(id)};
        return
                sqLiteDatabase.delete(DBUtils.DATABASE_TABLE,sql,contentValuesArray)>0;
    }
    //修改數(shù)據(jù)
    public boolean updateData(String id,String content,String userYear){
        ContentValues contentValues=new ContentValues();
        contentValues.put(DBUtils.NOTEPAD_CONTENT,content);
        contentValues.put(DBUtils.NOTEPAD_TIME,userYear);
        String sql=DBUtils.NOTEPAD_ID+"=?";
        String[] strings=new String[]{id};
        return
                sqLiteDatabase.update(DBUtils.DATABASE_TABLE,contentValues,sql,strings)>0;
    }
    //查詢數(shù)據(jù)
    public List<NotepadBean> query(){//將遍歷的數(shù)據(jù)存放在一個List<NotepadBean>類型的合集中
        List<NotepadBean> list=new ArrayList<NotepadBean>();
//        通過query()方法查詢數(shù)據(jù)庫表中的所有數(shù)據(jù),并返回一個Cursor對象
        Cursor cursor=sqLiteDatabase.query(DBUtils.DATABASE_TABLE,null,null,null,
                null,null,DBUtils.NOTEPAD_ID+" desc");
        if (cursor!=null){
            while (cursor.moveToNext()){//通過while循環(huán)遍歷Cursor對象中的數(shù)據(jù)
                NotepadBean noteInfo=new NotepadBean();
                String id = String.valueOf(cursor.getInt
                        (cursor.getColumnIndex(DBUtils.NOTEPAD_ID)));
                String content = cursor.getString(cursor.getColumnIndex
                        (DBUtils.NOTEPAD_CONTENT));
                String time = cursor.getString(cursor.getColumnIndex
                        (DBUtils.NOTEPAD_TIME));
                noteInfo.setId(id);
                noteInfo.setNotepadContent(content);
                noteInfo.setNotepadTime(time);
                list.add(noteInfo);
            }
            cursor.close();
        }
        return list;
    }
}

六、實(shí)現(xiàn)記事本界面的顯示功能NotepadAdapter.java?

1.包括顯示列表功能和 添加按鈕功能

2.步驟

初始化ListView控件,初始化添加按鈕

    listView = (ListView) findViewById(R.id.listview);
    ImageView add = (ImageView) findViewById(R.id.add);

創(chuàng)建initData()方法,在這個方法里初始化需要寫入的數(shù)據(jù)

    protected void initData() {
        mSQLiteHelper= new SQLiteHelper(this); //創(chuàng)建數(shù)據(jù)庫
        showQueryData();
   }

創(chuàng)建showQueryData()方法,在這個方法里獲取數(shù)據(jù)庫里面的數(shù)據(jù),并顯示在列表里面。
首先調(diào)用query()方法查詢數(shù)據(jù)庫中保存的數(shù)據(jù),返回值是list的集合。
接著設(shè)置一個適配器,將獲取的記錄數(shù)據(jù)傳遞到NotepadAdapter中,需要傳入兩個參數(shù),一個是上下文的信息,第二個是一個集合。
最后通過setAdapter()方法為ListView控件設(shè)置NotepadAdapter適配器。

list = mSQLiteHelper.query();
adapter = new NotepadAdapter(this, list);
listView.setAdapter(adapter);

initData()方法在onCreat()方法中調(diào)用一下。

為ImageView設(shè)置點(diǎn)擊事件
創(chuàng)建intent對象,需傳入兩個參數(shù),一個是上下文的信息,第二個是跳轉(zhuǎn)的activity的名稱。
調(diào)用startActivityForResult()方法跳轉(zhuǎn)到添加記錄界面。

add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NotepadActivity.this,
                        RecordActivity.class);
                startActivityForResult(intent, 1);
            }
        });

重寫onActivityResult()方法,當(dāng)關(guān)閉添加記錄界面時,調(diào)用該方法。

首先判斷請求碼是否為1 返回碼是否為2(這是在添加記錄界面設(shè)置的),是,調(diào)用showQueryData()方法重新獲取數(shù)據(jù)并顯示。

protected void onActivityResult(int requestCode,int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            showQueryData();
        }
    }

3.NotepadAdapter.java?具體代碼如下

package cn.itcast.notepad;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.List;

import cn.itcast.notepad.adapter.NotepadAdapter;
import cn.itcast.notepad.bean.NotepadBean;
import cn.itcast.notepad.database.SQLiteHelper;

public class NotepadActivity extends Activity {
    ListView listView;
    List<NotepadBean> list;
    SQLiteHelper mSQLiteHelper;
    NotepadAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notepad);
        //用于顯示便簽的列表
        listView = (ListView) findViewById(R.id.listview);
        ImageView add = (ImageView) findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NotepadActivity.this,
                        RecordActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        initData();
    }
    protected void initData() {
        mSQLiteHelper= new SQLiteHelper(this); //創(chuàng)建數(shù)據(jù)庫
        showQueryData();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id){
                NotepadBean notepadBean = list.get(position);
                Intent intent = new Intent(NotepadActivity.this, RecordActivity.class);
                intent.putExtra("id", notepadBean.getId());
                intent.putExtra("time", notepadBean.getNotepadTime()); //記錄的時間
                intent.putExtra("content", notepadBean.getNotepadContent()); //記錄的內(nèi)容
                NotepadActivity.this.startActivityForResult(intent, 1);
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int
                    position, long id) {
                AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder( NotepadActivity.this)
                        .setMessage("是否刪除此事件?")
                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                NotepadBean notepadBean = list.get(position);
                                if(mSQLiteHelper.deleteData(notepadBean.getId())){
                                    list.remove(position);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(NotepadActivity.this,"刪除成功",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                dialog =  builder.create();
                dialog.show();
                return true;
            }
        });

    }
    private void showQueryData(){
        if (list!=null){
            list.clear();
        }
        //從數(shù)據(jù)庫中查詢數(shù)據(jù)(保存的標(biāo)簽)
        list = mSQLiteHelper.query();
        adapter = new NotepadAdapter(this, list);
        listView.setAdapter(adapter);
    }
    @Override
    protected void onActivityResult(int requestCode,int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==1&&resultCode==2){
            showQueryData();
        }
    }
}

七、搭建添加記錄界面和修改記錄界面的布局activity_record.xml?

當(dāng)點(diǎn)擊記事本界面的“添加”按鈕時,會跳轉(zhuǎn)到添加記錄界面,當(dāng)點(diǎn)擊記事本界面列表中的item時,會跳轉(zhuǎn)到修改記錄界面。由于這兩個界面上的控件與功能基本相同,因此設(shè)置同一個Activity和同一個布局文件顯示。

1.選中cn.itcast.notepad包,右擊-【New】-【Activity】,命名為RecordActivity

android記事本app代碼,AndroidStudio,android,android studio

2.activity_record.xml代碼如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fefefe">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="#fb7a6a"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/note_back"
            android:layout_width="45dp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:paddingLeft="11dp"
            android:src="@drawable/back" />
        <TextView
            android:id="@+id/note_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:gravity="center"
            android:text="記事本"
            android:textColor="@android:color/white"
            android:textSize="15sp"
            android:textStyle="bold" />
    </RelativeLayout>
    <TextView
        android:id="@+id/tv_time"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="15sp"
        android:paddingTop="10dp"
        android:paddingBottom="10dp"
        android:gravity="center"
        android:visibility="gone"
        android:textColor="#fb7a6a"/>
    <EditText
        android:id="@+id/note_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="top"
        android:hint="請輸入要添加的內(nèi)容"
        android:paddingLeft="5dp"
        android:textColor="@android:color/black"
        android:background="#fefefe" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#fb7a6a"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:orientation="horizontal">
        <ImageView
            android:id="@+id/delete"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:src="@drawable/delete"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>
        <ImageView
            android:id="@+id/note_save"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:src="@drawable/save_note"
            android:paddingBottom="15dp"
            android:paddingTop="9dp"/>
    </LinearLayout>
</LinearLayout>

八、實(shí)現(xiàn)添加記錄界面的功能RecordActivity.java?

1.包括編輯記錄、保存記錄、清除記錄功能

2.步驟

初始化界面控件,設(shè)置點(diǎn)擊事件。

switch通過id判斷被點(diǎn)擊的按鈕屬于哪個控件。
當(dāng)選擇“保存”按鈕時,首先要獲取輸入框中的內(nèi)容getText(),將文本信息轉(zhuǎn)換為字符串toString(),再將空字符串清除掉trim()。
接著判斷輸入的內(nèi)容是否>0,如果大于0,調(diào)用insertData()方法,將記錄添加到數(shù)據(jù)庫中,需傳入兩個參數(shù),一個是輸入的內(nèi)容,一個點(diǎn)擊保存按鈕的時間。
接下來判斷數(shù)據(jù)是否保存成功,如果成功,要彈出一個“保存成功”的提示。失敗,要彈出一個“保存失敗”的提示。所以需要創(chuàng)建一個showToast()方法。保存成功還要調(diào)用setResult()方法返回一個返回碼:2。

showToast()方法用于顯示一些信息,在這個方法中調(diào)用makeText()方法。

創(chuàng)建initData()方法,創(chuàng)建數(shù)據(jù)庫。

initData()方法在onCreat()方法中調(diào)用一下。

3.具體代碼RecordActivity.java

package cn.itcast.notepad;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import cn.itcast.notepad.database.SQLiteHelper;
import cn.itcast.notepad.utils.DBUtils;

public class RecordActivity extends Activity implements View.OnClickListener {
    ImageView note_back;
    TextView note_time;
    EditText content;
    ImageView delete;
    ImageView note_save;
    SQLiteHelper mSQLiteHelper;
    TextView noteName;
    String id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_record);
        note_back = (ImageView) findViewById(R.id.note_back);
        note_time = (TextView)findViewById(R.id.tv_time);
        content = (EditText) findViewById(R.id.note_content);
        delete = (ImageView) findViewById(R.id.delete);
        note_save = (ImageView) findViewById(R.id.note_save);
        noteName = (TextView) findViewById(R.id.note_name);
        note_back.setOnClickListener(this);
        delete.setOnClickListener(this);
        note_save.setOnClickListener(this);
        initData();
    }
    protected void initData() {
        mSQLiteHelper = new SQLiteHelper(this);
        noteName.setText("添加記錄");
        Intent intent = getIntent();
        if(intent!= null){
            id = intent.getStringExtra("id");
            if (id != null){
                noteName.setText("修改記錄");
                content.setText(intent.getStringExtra("content"));
                note_time.setText(intent.getStringExtra("time"));
                note_time.setVisibility(View.VISIBLE);
            }
        }
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.note_back:
                finish();
                break;
            case R.id.delete:
                content.setText("");
                break;
            case R.id.note_save:
                String noteContent=content.getText().toString().trim();
                if (id != null){//修改操作
                    if (noteContent.length()>0){
                        if (mSQLiteHelper.updateData(id, noteContent, DBUtils.getTime())){
                            showToast("修改成功");
                            setResult(2);
                            finish();
                        }else {
                            showToast("修改失敗");
                        }
                    }else {
                        showToast("修改內(nèi)容不能為空!");
                    }
                }else {
                    //向數(shù)據(jù)庫中添加數(shù)據(jù)
                    if (noteContent.length()>0){
                        if (mSQLiteHelper.insertData(noteContent, DBUtils.getTime())){
                            showToast("保存成功");
                            setResult(2);
                            finish();
                        }else {
                            showToast("保存失敗");
                        }
                    }else {
                        showToast("修改內(nèi)容不能為空!");
                    }
                }
                break;
        }
    }
    public void showToast(String message){
        Toast.makeText(RecordActivity.this,message,Toast.LENGTH_SHORT).show();
    }
}

九、實(shí)現(xiàn)修改記錄界面的功能?

(備注:前面代碼已經(jīng)包含此內(nèi)容,這里弄清楚就好)??

比添加記錄界面多了查看記錄和修改記錄的功能。

1.實(shí)現(xiàn)查看記錄功能。

(1)記事本界面列表的每個Item只顯示2行記錄信息,如果想要查看更多的記錄內(nèi)容,則需要點(diǎn)擊Item,進(jìn)入修改記錄界面進(jìn)行查看。

(2)在NotepadActivity的initDate()方法中,添加跳轉(zhuǎn)到修改記錄界面的代碼。
通過setOnItemClickListener()方法實(shí)現(xiàn)Item的點(diǎn)擊事件,點(diǎn)擊Item,會調(diào)用onItemClick()方法,在該方法中首先通過get()方法獲取對應(yīng)的Item數(shù)據(jù)。創(chuàng)建intent對象,需要傳入兩個參數(shù),一個是上下文信息,另一個是需要跳轉(zhuǎn)的activity的名稱。接著將這些數(shù)據(jù)通過putExtra()方法封裝到intent對象中,最后調(diào)用startActivityForResult()方法跳轉(zhuǎn)到修改記錄界面,請求碼設(shè)為1。

protected void initData() {
        ...
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent,View view,int position,long id){
                NotepadBean notepadBean = list.get(position);
                Intent intent = new Intent(NotepadActivity.this, RecordActivity.class);
                intent.putExtra("id", notepadBean.getId());
                intent.putExtra("time", notepadBean.getNotepadTime()); //記錄的時間
                intent.putExtra("content", notepadBean.getNotepadContent()); //記錄的內(nèi)容
                NotepadActivity.this.startActivityForResult(intent, 1);
            }
        });

(3)在RecordActivity中找到initData()方法,在該方法中接收記事本界面?zhèn)鬟f過來的記錄數(shù)據(jù)并顯示到界面上。
首先獲取intent對象,接著判斷對象是否為空。如果不為空,獲取傳遞的數(shù)據(jù)。
先獲取id ,判斷id是否為空,如果不為空,需要將標(biāo)題設(shè)為“修改記錄”。然后通過get分別獲取記錄時間、記錄內(nèi)容并通過set顯示,最后將記錄時間設(shè)置為顯示狀態(tài)。

protected void initData() {
        mSQLiteHelper = new SQLiteHelper(this);
        noteName.setText("添加記錄");
        Intent intent = getIntent();
        if(intent!= null){
            id = intent.getStringExtra("id");
            if (id != null){
                noteName.setText("修改記錄");
                content.setText(intent.getStringExtra("content"));
                note_time.setText(intent.getStringExtra("time"));
                note_time.setVisibility(View.VISIBLE);
            }
        }
    }

2.實(shí)現(xiàn)修改記錄功能

(1)在RecordActivity的onClick()方法中,找到“保存”按鈕的點(diǎn)擊事件,在該事件中,判斷傳遞過來的id是否為空,如果不為空,那就是修改記錄功能。將修改記錄的id、修改的內(nèi)容、保存修改記錄的時間傳遞到updateData()方法中,進(jìn)行修改。
如果為空,就是添加記錄功能。

(2)具體代碼:

public void onClick(View v) {
        switch (v.getId()) {
            ...
            case R.id.note_save:
                String noteContent=content.getText().toString().trim();
                if (id != null){//修改操作
                    if (noteContent.length()>0){
                        if (mSQLiteHelper.updateData(id, noteContent, DBUtils.getTime())){
                            showToast("修改成功");
                            setResult(2);
                            finish();
                        }else {
                            showToast("修改失敗");
                        }
                    }else {
                        showToast("修改內(nèi)容不能為空!");
                    }
                }else {
                    ...
        }
    }

十、刪除記事本中的記錄?

(備注:前面代碼已經(jīng)包含此內(nèi)容,這里弄清楚就好)?

(1)當(dāng)長按列表的Item,此時會彈出一個對話框提示是否刪除記錄,因此在NotepadActivity的initData()方法中要加上刪除記錄的代碼。

(2)首先,通過setOnItemLongClickListener()設(shè)置長按事件的監(jiān)聽器。當(dāng)長按Item,會調(diào)用onItemLongClick()方法,在該方法中實(shí)現(xiàn)長按事件。
創(chuàng)建一個AlertDialog對話框,用于提示用戶是否刪除。創(chuàng)建Builder對象,在這個對象中傳入上下文信息。

(3)NotepadActivity的initData()方法代碼:文章來源地址http://www.zghlxwxcb.cn/news/detail-779255.html

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int
                    position, long id) {
                AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder( NotepadActivity.this)
                        .setMessage("是否刪除此事件?")
                        .setPositiveButton("確定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                NotepadBean notepadBean = list.get(position);
                                if(mSQLiteHelper.deleteData(notepadBean.getId())){
                                    list.remove(position);
                                    adapter.notifyDataSetChanged();
                                    Toast.makeText(NotepadActivity.this,"刪除成功",
                                            Toast.LENGTH_SHORT).show();
                                }
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                dialog =  builder.create();
                dialog.show();
                return true;
            }
        });

到了這里,關(guān)于Android——記事本功能業(yè)務(wù)(完整代碼)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • Android Studio——記事本案例

    Android Studio——記事本案例

    一、布局界面 ? ? ? ? 1、記事本界面布局 main_notepad.xml ? ? ? ? 2、記事本Item布局界面 activity_item.xml ? ? ? ? 3、添加、修改界面布局 activity_record.xml 二、封裝記錄信息實(shí)體類 ? ? ? ? 記事本的每個記錄都會有記錄內(nèi)容和記錄時間這兩個屬性,因此需要建立一個實(shí)體類用于存

    2024年02月05日
    瀏覽(28)
  • 基于Android Studio 開發(fā)的簡易記事本

    基于Android Studio 開發(fā)的簡易記事本

    ?? 文章末尾有獲取完整項(xiàng)目源碼方式 ?? 目錄 一、引言 視頻效果展示: 圖片效果展示: 二、詳細(xì)設(shè)計(jì) 1.首頁 2.添加和修改頁面 3.登錄頁 4.注冊頁 三、獲取源碼 ? ? ? ? ?Android初學(xué)者開發(fā)第一個完整的基礎(chǔ)實(shí)例項(xiàng)目應(yīng)該就屬《記事本》了,該項(xiàng)目基于Android Studio開發(fā)使用

    2024年02月05日
    瀏覽(29)
  • 基于Android的記事本設(shè)計(jì)和模塊開發(fā)

    基于Android的記事本設(shè)計(jì)和模塊開發(fā)

    有一萬五千字論文,完美運(yùn)行。 由于編程技術(shù)的迅速發(fā)展,各種記事本APP隨處可見,在人們的日常生活中經(jīng)常使用的到。于是各種記事本APP也跟著發(fā)展起來。本文在通過在Android Studio開發(fā)平臺上開發(fā)一個簡單的多功能語音輸入記事本APP的過程,同時了解記事本APP的功能實(shí)現(xiàn),

    2024年02月03日
    瀏覽(33)
  • Android 備忘錄,記事本程序設(shè)計(jì)

    Android 備忘錄,記事本程序設(shè)計(jì)

    android備忘錄實(shí)現(xiàn),使用ObjectBox數(shù)據(jù)庫框架進(jìn)行數(shù)據(jù)存儲,增刪改查等操作。代碼使用kotlin編寫。 1、下面看看ObjectBox數(shù)據(jù)庫封裝 需要注意的是: ? ?/** ? ? ?* 你只有配置好之后, 點(diǎn)擊 Make Model \\\'你的model名字\\\', 才會創(chuàng)建 MyObjectBox對象 ? ? ?* 對于MyObjectBox的包名, 目前我發(fā)現(xiàn)的

    2024年01月23日
    瀏覽(27)
  • java記事本源代碼

    java記事本源代碼

    本文仿電腦自帶記事本,實(shí)現(xiàn)的功能有新建、新窗口、打開、保存、另存為、退出、撤銷、剪切、復(fù)制、粘貼、刪除、查找、查找下一個、查找上一個、替換、轉(zhuǎn)到、全選、時間/日期、自動換行、縮放(放大、縮小、恢復(fù)默認(rèn)大?。?,未實(shí)現(xiàn)功能有頁面設(shè)置、打印、字體、狀

    2024年02月10日
    瀏覽(18)
  • android studio大作業(yè),android studio課程設(shè)計(jì),記事本實(shí)現(xiàn)

    android studio大作業(yè),android studio課程設(shè)計(jì),記事本實(shí)現(xiàn)

    先看效果圖 功能點(diǎn)實(shí)現(xiàn): 登錄,注冊,記事本分類添加,刪除,數(shù)據(jù)分析統(tǒng)計(jì)報(bào)表,數(shù)據(jù)庫使用SQLlite 部分實(shí)現(xiàn)代碼

    2024年02月11日
    瀏覽(26)
  • 基于Android平臺的記事本軟件(Android Studio項(xiàng)目+報(bào)告+app文件)

    基于Android平臺的記事本軟件(Android Studio項(xiàng)目+報(bào)告+app文件)

    移動應(yīng)用開發(fā)技術(shù) 期末考核報(bào)告 題 ?? 目: ??????? 基于 Android 平臺的記事本軟件 ???????????? 學(xué)生姓名 ? ???????????????????????? ?? 學(xué)生學(xué)號 ? ???????????????????????? ?? 專 ? ?? 業(yè) ? ???????????????????????? 班 ??? 級

    2024年02月08日
    瀏覽(32)
  • 基于安卓系統(tǒng)(android)記事本APP管理系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)

    基于安卓系統(tǒng)(android)記事本APP管理系統(tǒng)設(shè)計(jì)與實(shí)現(xiàn)

    目錄 摘要 I Abstract II 1 緒論 1.1 課題來源、目的和意義 1 1.2 國內(nèi)外基本研究情況 1 2 需求分析 2.1 用戶需求 4 2.2 功能需求 4 2.3 數(shù)據(jù)庫選擇 6 2.4 性能需求 6 3 概要設(shè)計(jì) 3.1 功能概要設(shè)計(jì) 7 3.2 數(shù)據(jù)庫概要設(shè)計(jì) 13 4 詳細(xì)設(shè)計(jì) 4.1 功能設(shè)計(jì) 15 4.2 數(shù)據(jù)庫設(shè)計(jì) 30 5 系統(tǒng)功能實(shí)現(xiàn) 5.1 系統(tǒng)架

    2024年02月11日
    瀏覽(26)
  • 【Android 記事本,筆記本,可注冊登錄,增刪改查(附源碼)】

    【Android 記事本,筆記本,可注冊登錄,增刪改查(附源碼)】

    簡介 用Sqlite做數(shù)據(jù)庫,用來存儲賬號以及筆記信息,實(shí)現(xiàn)了賬號的注冊以及登錄功能,筆記方面實(shí)現(xiàn)了新增、刪除、修改、搜索功能,列表展示筆記使用的是listView(懶得弄分割線,就使用listView的默認(rèn)分割線了); 運(yùn)行效果 代碼講解 我代碼里使用了兩個依賴,一個是工具

    2024年02月04日
    瀏覽(20)
  • MFC第十九天 記事本項(xiàng)目功能完善和開發(fā)、CTabCtrl類與分頁模式開發(fā)

    MFC第十九天 記事本項(xiàng)目功能完善和開發(fā)、CTabCtrl類與分頁模式開發(fā)

    獲取選擇的文字 向下查找 查找替換功能 向下 向上 不區(qū)分大小寫的 替換當(dāng)前選中 替換全部 打開查找編輯框需要加載的 CFileDialog 構(gòu)造函數(shù)詳解 pch.h CApp NotePad.cpp 對編碼的解析 以及對編碼格式的轉(zhuǎn)換 CMainDlg.h CMainDlg.cpp CMainDlg.h CMainDlg.cpp CFileDialogXq.h CFileDialogXq.cpp CMainDlg.h CMai

    2024年02月16日
    瀏覽(27)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包