一、布局界面
? ? ? ? 1、記事本界面布局
main_notepad.xml
<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:background="@android:color/darker_gray"
android:text="記事本"
android:gravity="center"
android:textStyle="bold"
android:textColor="@android:color/black"
android:textSize="20sp" />
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#E4E4E4E4"
android:dividerHeight="1dp"
android:fadingEdge="horizontal"
android:listSelector="#00000000"
android:scrollbars="none"
android:layout_below="@+id/note_name">
</ListView>
<ImageView
android:id="@+id/add"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/add"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
? ? ? ? 2、記事本Item布局界面
activity_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="12dp">
<TextView
android:id="@+id/ietm_content"
android:textColor="@android:color/black"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="2"
android:ellipsize="end"
android:lineSpacingExtra="3dp"
android:paddingTop="10dp"/>
<TextView
android:id="@+id/item_time"
android:textColor="#fb7a6a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="5dp"
android:paddingBottom="7dp"/>
</LinearLayout>
? ? ? ? 3、添加、修改界面布局
activity_record.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#fefefe">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#E4E4E4"
android:orientation="horizontal">
<ImageView
android:id="@+id/note_back"
android:layout_width="45dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:background="@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/black"
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="請(qǐng)輸入要添加的內(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:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
<ImageView
android:id="@+id/delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/delete"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
<ImageView
android:id="@+id/note_save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/save"
android:paddingTop="9dp"
android:paddingBottom="15dp" />
<ImageView
android:id="@+id/none2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/white"
android:paddingBottom="15dp"
android:paddingTop="9dp"/>
</LinearLayout>
</LinearLayout>
二、封裝記錄信息實(shí)體類
? ? ? ? 記事本的每個(gè)記錄都會(huì)有記錄內(nèi)容和記錄時(shí)間這兩個(gè)屬性,因此需要建立一個(gè)實(shí)體類用于存放這些屬性。
NotepadBean.java
public class NotepadBean {
private String id;
private String notepadContent;
private String notepadTime;
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;
}
}
三、編寫記事本界面列表適配器
? ? ? ? 因?yàn)橛浭卤镜募o(jì)錄列表是使用ListView控件展示的,因此需要建立一個(gè)數(shù)據(jù)適配器對(duì)ListView控件進(jìn)行數(shù)據(jù)適配。
NotepadAdapter.java
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
//獲取Item條目的總數(shù)
public int getCount(){
return list==null? 0: list.size();
}
@Override
//根據(jù)position(位置)獲取某個(gè)Item的對(duì)象
public Object getItem(int position){
return list.get(position);
}
@Override
//根據(jù)position(位置)獲取某個(gè)Item的id
public long getItemId(int position){
return position;
}
@Override
//獲取相應(yīng)position對(duì)應(yīng)的Item視圖,position是當(dāng)前Item的位置,convertView用于復(fù)用舊視圖,parent用于加載xml布局
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder viewHolder;
//通過inflate()方法加載Item布局,并將獲取的數(shù)據(jù)顯示到對(duì)應(yīng)的控件上,并判斷舊視圖是否為空,若為空,則創(chuàng)建一個(gè)ViewHolder對(duì)象
//通過set.Tag()方法將該對(duì)象添加到convertView中進(jìn)行緩存,否則把獲取的舊視圖進(jìn)行緩存
if(convertView==null){
convertView=layoutInflater.inflate(R.layout.activity_item,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.ietm_content);
tvNotepadTime=(TextView) view.findViewById((R.id.item_time));
}
}
}
四、創(chuàng)建數(shù)據(jù)庫(kù)
? ? ? ? 因?yàn)橛浭卤镜拇鎯?chǔ)和讀取記錄的數(shù)據(jù)都是通過數(shù)據(jù)庫(kù)完成的,因此需要?jiǎng)?chuàng)建數(shù)據(jù)庫(kù)
? ? ? ? 1、先創(chuàng)建一個(gè)DBUtils類,在該類中定義數(shù)據(jù)庫(kù)的名稱、表名、數(shù)據(jù)庫(kù)版本、數(shù)據(jù)庫(kù)表中的列名以及獲取當(dāng)前日期等信息。
DBUtils.java
public class DBUtils {
public static final String DATABASE_NAME="Notepad";
public static final String DATABASE_TABLE="Note";
public static final int DATABASE_VERSION=1;
//數(shù)據(jù)庫(kù)表中的列名
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);
}
}
? ? ? ? 2、創(chuàng)建SQLiteHelper類,在該類中實(shí)現(xiàn)增刪改查操作
SQLiteHelper.java
public class SQLiteHelper extends SQLiteOpenHelper {
private SQLiteDatabase sqLiteDatabase;
//創(chuàng)建數(shù)據(jù)庫(kù)
public SQLiteHelper(Context context){
super(context, DBUtils.DATABASE_NAME,null,DBUtils.DATABASE_VERSION);
sqLiteDatabase=this.getWritableDatabase();
}
//創(chuàng)建表
@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(){
List<NotepadBean> list =new ArrayList<NotepadBean>();
Cursor cursor=sqLiteDatabase.query(DBUtils.DATABASE_TABLE,null,null,null,null,null,DBUtils.NOTEPAD_ID+" desc");
if(cursor!=null){
while(cursor.moveToNext()){
NotepadBean noteInfo=new NotepadBean();
@SuppressLint("range")
String id=String.valueOf(cursor.getInt(cursor.getColumnIndex(DBUtils.NOTEPAD_ID)));
@SuppressLint("range")
String content=cursor.getString(cursor.getColumnIndex(DBUtils.NOTEPAD_CONTENT));
@SuppressLint("range")
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)添加記錄界面功能
? ? ? ? 因?yàn)樘砑佑涗浗缑嬗斜4婧蛣h除按鈕,因此創(chuàng)建記錄類實(shí)現(xiàn)點(diǎn)擊事件,將編寫的內(nèi)容添加到數(shù)據(jù)庫(kù)中。
RecordActivity.java
public class RecordActivity extends AppCompatActivity implements View.OnClickListener{
ImageView delete,note_save,note_back;
TextView noteName,note_time;
EditText content;
SQLiteHelper mSQLiteHelper;
String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_record);
//通過findViewById獲取界面控件
note_back=(ImageView) findViewById(R.id.note_back);
note_save=(ImageView) findViewById(R.id.note_save);
delete=(ImageView) findViewById(R.id.delete);
noteName=(TextView) findViewById(R.id.note_name);
note_time=(TextView) findViewById(R.id.tv_time);
content=(EditText) findViewById(R.id.note_content);
//設(shè)置啟動(dòng)器
note_back.setOnClickListener(this);
delete.setOnClickListener(this);
note_save.setOnClickListener(this);
//初始化
initData();
}
protected void initData(){
//創(chuàng)建數(shù)據(jù)庫(kù)
mSQLiteHelper=new SQLiteHelper(this);
noteName.setText("添加記錄");
//接收記事本傳來的消息
//getIntent()方法獲取Intent對(duì)象
Intent intent=getIntent();
if(intent!=null){
//獲取傳遞的記錄id
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);
}
}
}
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:
//通過getText()獲取輸入內(nèi)容
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)容不能為空");
}
}
//向數(shù)據(jù)庫(kù)添加數(shù)據(jù)
else{
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ìn)入程序的界面的選擇與操作
MainActivity.java
public class MainActivity 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);
//設(shè)置添加按鈕事件
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//使用顯示Intent指定要跳轉(zhuǎn)的目標(biāo)Activity,并通過startActivityForResult()方法開啟目標(biāo)Activity
Intent intent=new Intent(MainActivity.this,RecordActivity.class);
startActivityForResult(intent,1);
}
});
initData();
}
protected void initData() {
//創(chuàng)建數(shù)據(jù)庫(kù)
mSQLiteHelper = new SQLiteHelper(this);
showQueryData();
//處理查看記事本詳細(xì)信息
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//通過get方法獲取對(duì)應(yīng)的Item數(shù)據(jù)
NotepadBean notepadBean=list.get(position);
//通過putExtra()方法封裝到Intent對(duì)象中
Intent intent=new Intent(MainActivity.this,RecordActivity.class);
intent.putExtra("id",notepadBean.getId());
intent.putExtra("time",notepadBean.getNotepadTime());
intent.putExtra("content",notepadBean.getNotepadContent());
MainActivity.this.startActivityForResult(intent,1);
}
});
//刪除記事本記錄
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
AlertDialog dialog;
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this)
.setMessage("是否刪除此紀(jì)錄?")
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//獲取Item對(duì)象
NotepadBean notepadBean=list.get(position);
if(mSQLiteHelper.deleteData(notepadBean.getId())){
//刪除對(duì)應(yīng)的Item
list.remove(position);
//更新記事本界面
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this,"刪除成功",Toast.LENGTH_SHORT).show();
}
}
})
.setPositiveButton("取消", 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ù)庫(kù)中查詢數(shù)據(jù)
list=mSQLiteHelper.query();
adapter=new NotepadAdapter(this,list);
listView.setAdapter(adapter);
}
@Override
//重寫,當(dāng)關(guān)閉添加記錄界面時(shí),程序回調(diào)該方法
// 并在該方法中調(diào)用showQueryData()方法重新獲取數(shù)據(jù)庫(kù)中保存的記錄數(shù)據(jù)并顯示到記錄列表中
protected void onActivityResult(int requestCode,int resultCode,Intent data){
super.onActivityResult(requestCode,resultCode,data);
if(requestCode==1&&resultCode==2){
showQueryData();
}
}
}
七、運(yùn)行結(jié)果截圖
? ? ? ? 主界面
? ? ? ? ?選擇添加按鈕之后進(jìn)入編寫界面
? ? ? ? ?修改界面文章來源:http://www.zghlxwxcb.cn/news/detail-448637.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-448637.html
到了這里,關(guān)于Android Studio——記事本案例的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!