Android實現(xiàn)簡易記事本
本文實例為大家分享了Android實現(xiàn)簡易記事本的具體代碼,供大家參考,具體內(nèi)容如下
下面實現(xiàn)了一個簡易的記事本,效果如下:
主要使用數(shù)據(jù)庫存儲數(shù)據(jù)。
完整代碼鏈接:Android實現(xià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); //選擇日期,添加,查詢的監(jiān)聽事件 chooseDate.setOnClickListener(this); add.setOnClickListener(this); query.setOnClickListener(this); //查詢情況默認隱藏,只有點擊查詢時才有效果 title.setVisibility(View.INVISIBLE); } @Override public void onClick(View v) { //創(chuàng)建數(shù)據(jù)庫 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è)置在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ù)庫中,添加方法是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: //查詢時顯示結(jié)果設(shè)置為visible title.setVisibility(View.VISIBLE); //查詢的結(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ù)庫中的方法 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(""); } //查詢的方法,返回值為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 + "%"});//對表的查詢(insert的操作) return cursor; } protected void onDestroy() { super.onDestroy(); if (mMyDataBaseHelper != null) { mMyDataBaseHelper.close(); } } }
MyDataBaseHelper:
public class MyDataBaseHelper extends SQLiteOpenHelper { //創(chuàng)建數(shù)據(jù)庫的語句 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); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter利用Canvas模擬實現(xiàn)微信紅包領(lǐng)取效果
這篇文章主要為大家詳細介紹了如何利用Flutter中的Canvas模擬實現(xiàn)微信紅包領(lǐng)取的效果,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-03-03Android?利用ImageView屬性實現(xiàn)選中和未選中效果
這篇文章主要介紹了Android巧用ImageView屬性實現(xiàn)選中和未選中效果,實現(xiàn)思路通常我們會選擇在布局里加個ImageView,然后通過代碼層面加個判斷去讓ImageView加載不同狀態(tài)的圖片,需要的朋友可以參考下2023-06-06Android 使用FragmentTabhost代替Tabhost
這篇文章主要介紹了Android 使用FragmentTabhost代替Tabhost的相關(guān)資料,需要的朋友可以參考下2017-05-05詳解Android中visibility屬性VISIBLE、INVISIBLE、GONE的區(qū)別
在Android開發(fā)中,大部分控件都有visibility這個屬性,其屬性有3個分別為“visible ”、“invisible”、“gone”。主要用來設(shè)置控制控件的顯示和隱藏。本文就詳細的講解一下。2016-12-12深入Android Handler,MessageQueue與Looper關(guān)系
這篇文章主要介紹了深入Android Handler,MessageQueue與Looper關(guān)系,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08