??文章末尾有獲取完整項目源碼方式??
目錄
一、引言
視頻效果展示:
圖片效果展示:
二、詳細設計
1.首頁
2.添加和修改頁面
3.登錄頁
4.注冊頁
三、獲取源碼
一、引言
? ? ? ? ?Android初學者開發(fā)第一個完整的基礎實例項目應該就屬《記事本》了,該項目基于Android Studio開發(fā)使用Java語言,該項目包含SQLlit數(shù)據(jù)庫的使用、listview、等。話不多說先上成品:
視頻效果展示:
基于Android Studio 開發(fā)的簡易記事本
圖片效果展示:
登錄頁
注冊頁?
首頁
添加頁面?
修改頁面
二、詳細設計
1.首頁
? ? ? ? ? 用戶進行筆記添加,使用listview顯示筆記。??
????????該代碼實現(xiàn)了一個記事本應用的主活動(NotepadActivity)。在該活動中,使用了一個ListView來顯示便簽的列表。主要包括以下幾個部分:
-
onCreate()方法:在該方法中,設置了活動的布局文件(activity_notepad.xml),并初始化了ListView和一個添加按鈕(ImageView)。添加按鈕的點擊事件監(jiān)聽器被設置為打開另一個活動(RecordActivity)的意圖,并在該意圖中傳遞了一個請求碼(1)。
-
initData()方法:該方法被調(diào)用來初始化數(shù)據(jù)。在該方法中,清空了列表數(shù)據(jù)(如果存在),并從SQLite數(shù)據(jù)庫中查詢數(shù)據(jù),并將查詢結果傳遞給自定義的適配器(NotepadAdapter)。最后,將適配器設置給ListView。
-
showQueryData()方法:該方法用于顯示查詢的數(shù)據(jù)。在該方法中,先清空列表數(shù)據(jù)(如果存在),然后再從SQLite數(shù)據(jù)庫中查詢數(shù)據(jù),并將查詢結果傳遞給適配器,最后將適配器設置給ListView。
-
onActivityResult()方法:當從另一個活動(RecordActivity)返回時,該方法會被調(diào)用。在該方法中,檢查返回的請求碼和結果碼,如果滿足條件,則調(diào)用showQueryData()方法來顯示查詢的數(shù)據(jù)。
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();
}
}
private void showQueryData(){
if (list!=null){
list.clear();
}
//從數(shù)據(jù)庫中查詢數(shù)據(jù)(保存的標簽)
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();
}
}
}
2.添加和修改頁面
? ? ? ? 點擊首頁加號按鈕或者點擊列表項按鈕進行對應的頁面跳轉。
?? ? ? ?
這段代碼是一個Android應用程序中的一部分,用于初始化數(shù)據(jù)和處理點擊事件。下面對代碼進行簡要的概括:
-
initData()
?方法用于初始化數(shù)據(jù)。首先創(chuàng)建一個?SQLiteHelper
?對象用于操作數(shù)據(jù)庫。然后根據(jù)傳入的?Intent
?對象獲取傳遞過來的數(shù)據(jù),并根據(jù)數(shù)據(jù)的有無設置相應的文本內(nèi)容和可見性。 -
onClick()
?方法是一個點擊事件的處理方法。根據(jù)點擊的控件的id,執(zhí)行相應的操作。-
當點擊了id為?
note_back
?的控件時,調(diào)用?finish()
?方法結束當前Activity。 -
當點擊了id為?
delete
?的控件時,將?content
?控件的文本內(nèi)容置空。 -
當點擊了id為?
note_save
?的控件時,首先獲取?content
?控件的文本內(nèi)容,并進行相應的操作。- 如果?
id
?不為?null
,表示是修改操作。判斷內(nèi)容是否為空,若不為空,則調(diào)用?mSQLiteHelper
?的?updateData()
?方法進行數(shù)據(jù)庫的更新操作,并根據(jù)更新結果顯示相應的提示信息。 - 如果?
id
?為?null
,表示是添加操作。判斷內(nèi)容是否為空,若不為空,則調(diào)用?mSQLiteHelper
?的?insertData()
?方法進行數(shù)據(jù)庫的插入操作,并根據(jù)插入結果顯示相應的提示信息。
- 如果?
-
????????這段代碼的主要目的是根據(jù)傳入的數(shù)據(jù)進行新增或修改數(shù)據(jù)庫中的記錄,并根據(jù)操作結果顯示相應的提示信息。
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;
}
3.登錄頁
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fb7a6a"
tools:context=".Login.LoginActivity">
<LinearLayout
android:layout_width="300dp"
android:layout_height="0dp"
android:gravity="center|top"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView">
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入賬號"
android:textSize="20sp" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="請輸入密碼"
android:inputType="textPassword"
android:textSize="20sp" />
<Button
android:id="@+id/login_button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:background="@drawable/button"
android:text="立 即 登 陸"
android:textColor="#fff"
android:textSize="14sp" />
<Button
android:id="@+id/register_button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/button"
android:text="立 即 注 冊"
android:textColor="#fff"
android:textSize="14sp" />
</LinearLayout>
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
</androidx.constraintlayout.widget.ConstraintLayout>
4.注冊頁
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fb7a6a"
tools:context=".Register.RegisterActivity">
<LinearLayout
android:layout_width="300dp"
android:layout_height="0dp"
android:gravity="center|top"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/imageView2">
<EditText
android:id="@+id/username_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入賬號"
android:textSize="20sp" />
<EditText
android:id="@+id/password_edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="請輸入密碼"
android:inputType="textPassword"
android:textSize="20sp" />
<EditText
android:id="@+id/repeat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="再次輸入您的密碼"
android:inputType="textPassword"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center|right"
android:text="已有賬號?立即登錄"
android:textColor="#fff"
android:textSize="15sp" />
<Button
android:id="@+id/register_button"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:background="@drawable/button"
android:text="立 即 注 冊"
android:textColor="#fff"
android:textSize="14sp" />
</LinearLayout>
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/logo" />
</androidx.constraintlayout.widget.ConstraintLayout>
三、獲取源碼
關注公眾號《編程樂學》,后臺回復:23050901文章來源:http://www.zghlxwxcb.cn/news/detail-753180.html
????????????????????快捷獲取方式??????文章來源地址http://www.zghlxwxcb.cn/news/detail-753180.html
到了這里,關于基于Android Studio 開發(fā)的簡易記事本的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!