Android實(shí)現(xiàn)簡(jiǎn)易記事本
本文實(shí)例為大家分享了Android實(shí)現(xiàn)簡(jiǎn)易記事本的具體代碼,供大家參考,具體內(nèi)容如下
下面實(shí)現(xiàn)了一個(gè)簡(jiǎn)易的記事本,效果如下:
主要使用數(shù)據(jù)庫(kù)存儲(chǔ)數(shù)據(jù)。
完整代碼鏈接:Android實(shí)現(xiàn)簡(jiǎn)易記事本
下面是部分代碼:
MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private EditText subject; private EditText body; private EditText date; private Button chooseDate; private Button add; private Button query; private ListView result; private LinearLayout title; private MyDataBaseHelper mMyDataBaseHelper; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { //控件初始化 subject=(EditText)findViewById(R.id.subject); body=(EditText)findViewById(R.id.body); date=(EditText)findViewById(R.id.date); chooseDate=(Button)findViewById(R.id.chooseDate); add=(Button)findViewById(R.id.add); query=(Button)findViewById(R.id.query); result=(ListView)findViewById(R.id.result); title=(LinearLayout)findViewById(R.id.title); //選擇日期,添加,查詢(xún)的監(jiān)聽(tīng)事件 chooseDate.setOnClickListener(this); add.setOnClickListener(this); query.setOnClickListener(this); //查詢(xún)情況默認(rèn)隱藏,只有點(diǎn)擊查詢(xún)時(shí)才有效果 title.setVisibility(View.INVISIBLE); } @Override public void onClick(View v) { //創(chuàng)建數(shù)據(jù)庫(kù) mMyDataBaseHelper=new MyDataBaseHelper(MainActivity.this,"memento.db",null,1); SQLiteDatabase sqLiteDatabase=mMyDataBaseHelper.getReadableDatabase(); //獲取輸入框的內(nèi)容 String strSubject=subject.getText().toString().trim(); String strBody=body.getText().toString().trim(); String strDate=date.getText().toString().trim(); switch (v.getId()){ case R.id.chooseDate: Calendar calendar=Calendar.getInstance(); //選擇時(shí)間,并將時(shí)間設(shè)置在date中 new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) { date.setText(year+"-"+month+"-"+dayOfMonth); } },calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show(); break; case R.id.add: //添加內(nèi)容到數(shù)據(jù)庫(kù)中,添加方法是add title.setVisibility(View.INVISIBLE); add(sqLiteDatabase,strSubject,strBody,strDate); Toast.makeText(this, "success!", Toast.LENGTH_SHORT).show(); result.setAdapter(null); break; case R.id.query: //查詢(xún)時(shí)顯示結(jié)果設(shè)置為visible title.setVisibility(View.VISIBLE); //查詢(xún)的結(jié)果保存在cursor中 Cursor cursor=query(sqLiteDatabase,strSubject,strBody,strDate); SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(MainActivity.this,R.layout.result,cursor,new String[]{"_id", "subject", "body", "date"}, new int[]{R.id.memento_num, R.id.memento_subject, R.id.memento_body, R.id.memento_date}); //為listview添加適配器 result.setAdapter(simpleCursorAdapter); break; } } //添加數(shù)據(jù)到數(shù)據(jù)庫(kù)中的方法 public void add(SQLiteDatabase sqLiteDatabase,String subject,String body,String date){ sqLiteDatabase.execSQL("Insert into memento_tb values(null,?,?,?)",new String[]{subject,body,date}); //初始設(shè)置為空 this.subject.setText(""); this.body.setText(""); this.date.setText(""); } //查詢(xún)的方法,返回值為cursor public Cursor query(SQLiteDatabase sqLiteDatabase, String subject, String body, String date){ Cursor cursor = sqLiteDatabase.rawQuery( "select * from memento_tb where subject like ? and body like ? and date like ? ", new String[]{"%" + subject + "%", "%" + body + "%", "%" + date + "%"});//對(duì)表的查詢(xún)(insert的操作) return cursor; } protected void onDestroy() { super.onDestroy(); if (mMyDataBaseHelper != null) { mMyDataBaseHelper.close(); } } }
MyDataBaseHelper:
public class MyDataBaseHelper extends SQLiteOpenHelper { //創(chuàng)建數(shù)據(jù)庫(kù)的語(yǔ)句 final String CREATE_TABLE_SQL = "create table memento_tb(_id integer primary " + "key autoincrement,subject,body,date)"; //建表 public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } //執(zhí)行建表的操作 @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_TABLE_SQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { System.out.println("---------" + oldVersion + "------->" + newVersion); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)記事本小功能
- Android記事本項(xiàng)目開(kāi)發(fā)
- Android實(shí)現(xiàn)記事本功能
- Android實(shí)現(xiàn)簡(jiǎn)易記事本
- android實(shí)現(xiàn)記事本app
- Android+SQLite數(shù)據(jù)庫(kù)實(shí)現(xiàn)的生詞記事本功能實(shí)例
- Android中實(shí)現(xiàn)記事本動(dòng)態(tài)添加行效果
- Android實(shí)現(xiàn)記事本功能(26)
- Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)
- Android手機(jī)開(kāi)發(fā)設(shè)計(jì)之記事本功能
相關(guān)文章
Flutter利用Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取效果
這篇文章主要為大家詳細(xì)介紹了如何利用Flutter中的Canvas模擬實(shí)現(xiàn)微信紅包領(lǐng)取的效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-03-03Android?利用ImageView屬性實(shí)現(xiàn)選中和未選中效果
這篇文章主要介紹了Android巧用ImageView屬性實(shí)現(xiàn)選中和未選中效果,實(shí)現(xiàn)思路通常我們會(huì)選擇在布局里加個(gè)ImageView,然后通過(guò)代碼層面加個(gè)判斷去讓ImageView加載不同狀態(tài)的圖片,需要的朋友可以參考下2023-06-06Android實(shí)現(xiàn)環(huán)形進(jìn)度條的實(shí)例
本文通過(guò)實(shí)例代碼給大家介紹了android實(shí)現(xiàn)環(huán)形進(jìn)度條的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),需要的朋友參考下2017-01-01Android 使用FragmentTabhost代替Tabhost
這篇文章主要介紹了Android 使用FragmentTabhost代替Tabhost的相關(guān)資料,需要的朋友可以參考下2017-05-05Android簡(jiǎn)單封裝一個(gè)MVP基類(lèi)流程詳解
MVP是從經(jīng)典的模式MVC演變而來(lái),它們的基本思想有相通的地方:Controller/Presenter負(fù)責(zé)邏輯的處理,Model提供數(shù)據(jù),View負(fù)責(zé)顯示。下面這篇文章主要給大家介紹了關(guān)于Android從實(shí)現(xiàn)到封裝MVP的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧2023-03-03詳解Android中visibility屬性VISIBLE、INVISIBLE、GONE的區(qū)別
在Android開(kāi)發(fā)中,大部分控件都有visibility這個(gè)屬性,其屬性有3個(gè)分別為“visible ”、“invisible”、“gone”。主要用來(lái)設(shè)置控制控件的顯示和隱藏。本文就詳細(xì)的講解一下。2016-12-12android自定義view實(shí)現(xiàn)鐘表效果
這篇文章主要為大家詳細(xì)介紹了android自定義view實(shí)現(xiàn)鐘表效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-12-12Android Fragment多層嵌套重影問(wèn)題的解決方法
這篇文章主要介紹了Android Fragment多層嵌套重影問(wèn)題的解決方法,從解決bug的思想,導(dǎo)致原因,原理解析等方面找出問(wèn)題所在原因,最終解決方法就可以簡(jiǎn)單了,對(duì)fragment 多層嵌套問(wèn)題感興趣的朋友一起通過(guò)本文學(xué)習(xí)吧2016-08-08深入Android Handler,MessageQueue與Looper關(guān)系
這篇文章主要介紹了深入Android Handler,MessageQueue與Looper關(guān)系,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-08