《移动应用程序设计基础》期末报告
课题名称:
《移动应用开发基础》上机考核
所使用的工具软件及环境:
JDK,Android Studio
一、课题背景
期末上机考核,将实验四中的内容和实验五的内容以及上课所完成的小实验相互结合,完成最后的考核。
二、任务内容
考核内容:上机单独完成一个简单日记本程序的编写。界面包括:用户登录,用户注册,日记本主页,新增编辑日记。
注意:以下***为您姓名的拼音单词第一个字母(如俞成海——ych)。
1.用户登录
布局:activity_login.xml, 布局如下图,资源图片username.png和password.png,布局要求左边内嵌图片,提示如图。(10分)
编辑
图1 登录界面布局
实现功能:LoginActivity.java
- 输入字段包括用户名(username),密码(password)和确定(ok)按钮,点击确定(ok)按钮(10分)
- 如果用户名(username)和密码(password)在SharedPreferences中存在的话,则跳转DiaryActivity;
- 点击注册,跳转用户注册界面RegisterActivity
- 如果用户名(username)和密码(password)在SharedPreferences中不存在或不一致,则跳转用户注册界面RegisterActivity,同时Toast提示“username,请先注册您的信息,***程序友情提示!“如下图。
编辑
注意:
- 注册时,写入SharePreferences,文件名user,保存值username和password;
- 登录时,读出SharePreferences(文件名为user)中的username和password
2.用户注册
界面布局:activity_register.xml, 布局如下图,资源图片username.png和password.png,布局要求左边内嵌图片,提示如下中间图。(10分)
编辑
功能实现:RegisterActivity.java。点击注册(register),将用户名(username)和密码(password)插入到写入SharePreferences中(文件名user,保存值username和password),返回到登录界面,Toast弹出“注册成功,请登陆,***程序友情提示!”,如上图。(10分)
3.日记主页
界面布局:activity_diary.xml, 布局如下图,包括AppBarLayout,Listview和FloatingActionButton。(10分)
编辑
功能实现:DiaryActivity.java,
- 获取数据库***_DiaryDB.db中的表diary的信息,并将日记数据显示在listview中。(10分)
- 点击列表项中的编辑图片按钮,界面跳转到日记新增编辑界面(DiaryInfoActivity)。(5分)
- 点击AppBar上的编辑(edit_tv)列表项中的编辑图片按钮(bianji.png)替换成删除图片(delete.png),编辑改成取消,如下图。点击删除图片按钮,删除列表中的这一项,同时删除数据库中的该条数据,点击取消,取消改回为编辑,列表项中的删除图片(delete.png)替换成编辑图片按钮(bianji.png)。(10分)
三、界面设计与实现(描述设计思想和理念的加分)
登录界面采用简单而常用的linerLayout布局,注册界面同样也是
编辑
编辑
日记本主界面采用CoordinatorLayout布局、listview采用RelativeLayout布局
编辑
编辑
编辑日记界面与采用CoordinatorLayout布局
编辑
四、功能流程与实现(使用设计思想、设计模式、UML图和流程图加分)
流程图:
编辑
核心的java代码文件,以及根据实验5之后多出来的登录界面的布局文件,如下图为所有的文件详情。
编辑
代码文件:(代码文件有资源包,下载链接在文章最后)
//AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.sqlitedemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".Login" android:label="每日记" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Register"/> <activity android:name=".MainActivity"/> <activity android:name=".DiaryInfoActivity"/> </application> </manifest>
//Login.java package com.example.sqlitedemo; import android.content.Context; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; public class Login extends AppCompatActivity { static int p=0; static public EditText usenameEdtxt,passwordEdtxt; public Button registerButton,submitbutton; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); usenameEdtxt=(EditText)findViewById(R.id.usename_edtxt); passwordEdtxt=(EditText)findViewById(R.id.password_edtxt); submitbutton=(Button)findViewById(R.id.submit_button); registerButton=(Button)findViewById(R.id.register_button); //登录按钮监控 submitbutton.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD) @Override public void onClick(View v) { String usename=usenameEdtxt.getText().toString(); String password=passwordEdtxt.getText().toString(); if(usename.isEmpty()){ usenameEdtxt.setError("用户名不能为空"); usenameEdtxt.requestFocus(); return; } if(password.isEmpty()){ passwordEdtxt.setError("密码不能为空"); passwordEdtxt.requestFocus(); return; } if(p==0){ Toast.makeText(com.example.sqlitedemo.Login.this, usename+"请先注册您的信息,邵永刚程序友情提示!", Toast.LENGTH_SHORT).show(); Intent it=new Intent(); Context packageContext; it.setClass(com.example.sqlitedemo.Login.this, Register.class); com.example.sqlitedemo.Login.this.startActivity(it); return; } if(usename.length()>0&&password.length()>0&&usename.equals(Register.usenameEdtxt1.getText().toString())&&p==1&&password.equals(Register.passwordEdtxt1.getText().toString())){ Toast.makeText(com.example.sqlitedemo.Login.this, "登录成功", Toast.LENGTH_SHORT).show(); Intent it=new Intent(); Context packageContext; it.setClass(com.example.sqlitedemo.Login.this, MainActivity.class); com.example.sqlitedemo.Login.this.startActivity(it); } else { Toast.makeText(com.example.sqlitedemo.Login.this, usename+"请先注册您的信息,邵永刚程序友情提示!", Toast.LENGTH_SHORT).show(); Intent it=new Intent(); Context packageContext; it.setClass(com.example.sqlitedemo.Login.this, Register.class); com.example.sqlitedemo.Login.this.startActivity(it); } } }); registerButton.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD) @Override public void onClick(View v) { Toast.makeText(com.example.sqlitedemo.Login.this, "进行注册", Toast.LENGTH_SHORT).show(); Intent it=new Intent(); Context packageContext; it.setClass(com.example.sqlitedemo.Login.this, Register.class); com.example.sqlitedemo.Login.this.startActivity(it); } }); } }
//Register.java package com.example.sqlitedemo; import android.content.Context; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; public class Register extends AppCompatActivity { static public EditText usenameEdtxt1,passwordEdtxt1; public Button registerButton; protected void onCreate(Bundle savedInstanceState) { Login.p=1; super.onCreate(savedInstanceState); setContentView(R.layout.register); usenameEdtxt1=(EditText)findViewById(R.id.usename_edtxt); passwordEdtxt1=(EditText)findViewById(R.id.password_edtxt); registerButton=(Button)findViewById(R.id.register_button); registerButton.setOnClickListener(new View.OnClickListener() { @RequiresApi(api = Build.VERSION_CODES.GINGERBREAD) @Override public void onClick(View v) { String usename=usenameEdtxt1.getText().toString(); String password=passwordEdtxt1.getText().toString(); if(usename.isEmpty()){ usenameEdtxt1.setError("用户名不能为空"); usenameEdtxt1.requestFocus(); return; } if(password.isEmpty()){ passwordEdtxt1.setError("密码不能为空"); passwordEdtxt1.requestFocus(); return; } if(usename.length()>0&&password.length()>0){ Toast.makeText(com.example.sqlitedemo.Register.this, "注册成功,请登陆,邵永刚程序友情提示!", Toast.LENGTH_SHORT).show(); Intent it=new Intent(); Context packageContext; it.setClass(com.example.sqlitedemo.Register.this, com.example.sqlitedemo.Login.class); com.example.sqlitedemo.Register.this.startActivity(it); } } }); } }
//MainActivity.java package com.example.sqlitedemo; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Build; import android.os.Bundle; import com.google.android.material.floatingactionbutton.FloatingActionButton; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.AdapterView; import android.widget.ListView; import android.widget.TextView; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import static android.os.Build.VERSION.SDK_INT; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; private TextView editTv; private ListView diaryList; private FloatingActionButton fab; private List<Map<String, String>> listData; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); listData = new ArrayList<>(); fab = (FloatingActionButton) findViewById(R.id.fab); editTv = (TextView) findViewById(R.id.edit_tv); diaryList = (ListView) findViewById(R.id.list_view); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.e(TAG, "onClick: " ); Intent intent = new Intent(MainActivity.this, DiaryInfoActivity.class); intent.putExtra("FLAG", 0); startActivity(intent); } }); editTv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e(TAG, "on" ); int length = queryData().size(); if (editTv.getText().toString().equals("取消")){ editTv.setText("编辑"); for (int i = 0;i<length;i++){ diaryList.getChildAt(i).findViewById(R.id.edit_ib).setVisibility(View.VISIBLE); diaryList.getChildAt(i).findViewById(R.id.delete_ib).setVisibility( View.INVISIBLE); } } else { editTv.setText("取消"); for (int i = 0;i<length;i++){ diaryList.getChildAt(i).findViewById(R.id.edit_ib).setVisibility(View.INVISIBLE); diaryList.getChildAt(i).findViewById(R.id.delete_ib).setVisibility( View.VISIBLE); } } } }); diaryList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(MainActivity.this,DiaryInfoActivity.class); intent.putExtra("FLAG",1); intent.putExtra(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE,listData.get(position).get(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE)); intent.putExtra(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT,listData.get(position).get(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT )); startActivity(intent); } }); diaryList.setAdapter(new DiaryListAdapter(MainActivity.this,queryData())); } @Override protected void onResume() { super.onResume(); //重新查询适配 diaryList.setAdapter(new DiaryListAdapter(MainActivity.this,queryData())); } /** * 从Sqlite中查询所有数据 * * @return 数据列表 */ public List<Map<String,String>> queryData(){ listData = new ArrayList<>(); DiaryDbHelper dbHelper = new DiaryDbHelper(this); SQLiteDatabase db = dbHelper.getReadableDatabase(); String[] projection = { SQLiteContract.DiaryEntry._ID,SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE, SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT,SQLiteContract.DiaryEntry.COLUMN_NAME_TIME }; String sortOrder = SQLiteContract.DiaryEntry.COLUMN_NAME_TIME+" DESC"; Cursor c = db.query(SQLiteContract.DiaryEntry.TABLE_NAME,projection,null,null, null,null,sortOrder); while (c.moveToNext()){ Map<String,String> map = new HashMap<>(); map.put(SQLiteContract.DiaryEntry._ID,c.getString(c.getColumnIndex( SQLiteContract.DiaryEntry._ID))); map.put(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE,c.getString(c.getColumnIndex(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE))); map.put(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT,c.getString(c .getColumnIndex(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT))); map.put(SQLiteContract.DiaryEntry.COLUMN_NAME_TIME,c.getString(c.getColumnIndex(SQLiteContract.DiaryEntry.COLUMN_NAME_TIME))); listData.add(map); } c.close(); db.close(); return listData; } }
//DiaryInfoActivity.java package com.example.sqlitedemo; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.MenuItem; import android.view.View; import android.widget.EditText; import android.widget.ImageButton; import android.widget.TextView; import android.widget.Toast; import android.widget.Toolbar; import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import java.lang.reflect.Method; import java.text.SimpleDateFormat; import java.util.Date; import static android.os.Build.VERSION.SDK_INT; public class DiaryInfoActivity extends AppCompatActivity { private TextView titleTv,contentTv; private EditText titleEt,contentEt; private ImageButton submitIb; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_diary_info); Log.e("TAG","onResume()"); androidx.appcompat.widget.Toolbar toolbar = (androidx.appcompat.widget.Toolbar) findViewById(R.id.toolbar_info); setSupportActionBar(toolbar); ActionBar actionBar = getSupportActionBar(); //添加返回按钮 actionBar.setDisplayHomeAsUpEnabled(true); //隐藏标题 actionBar.setDisplayShowTitleEnabled(false); titleTv = (TextView)findViewById(R.id.info_title_tv); contentTv = (TextView)findViewById(R.id.info_content_tv); titleEt = (EditText)findViewById(R.id.info_title_et); contentEt = (EditText)findViewById(R.id.info_content_et); submitIb = (ImageButton)findViewById(R.id.submit_ib); //获取传递的参数 int flag = getIntent().getIntExtra("FLAG",-1); final String id = getIntent().getStringExtra(SQLiteContract.DiaryEntry._ID); final String title = getIntent().getStringExtra(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE); String content = getIntent().getStringExtra(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT); //新建日记 if(flag==0){ submitIb.setVisibility(View.VISIBLE); titleTv.setVisibility(View.INVISIBLE); contentTv.setVisibility(View.INVISIBLE); titleEt.setVisibility(View.VISIBLE); contentEt.setVisibility(View.VISIBLE); submitIb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(titleEt==null || titleEt.getText().toString().trim().equals("")){ Toast.makeText(DiaryInfoActivity.this, "请输入标题", Toast.LENGTH_LONG).show(); return; } if(contentEt==null || contentEt.getText().toString().trim().equals("")){ Toast.makeText(DiaryInfoActivity.this, "请输入内容", Toast.LENGTH_LONG).show(); return; } //向Sqlite中插入一条信息 DiaryDbHelper dbHelper = new DiaryDbHelper(DiaryInfoActivity.this); SQLiteDatabase db = dbHelper.getWritableDatabase(); //格式化时间 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String date = sd.format(new Date()); //设置插入值 ContentValues values = new ContentValues(); values.put(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE, titleEt.getText().toString()); values.put(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT, contentEt.getText().toString()); values.put(SQLiteContract.DiaryEntry.COLUMN_NAME_TIME, date); //执行插入方法 long newRowId = db.insert(SQLiteContract.DiaryEntry.TABLE_NAME, null, values); DiaryInfoActivity.this.finish(); } }); } //查看日记 else if(flag==1){ submitIb.setVisibility(View.INVISIBLE); titleTv.setVisibility(View.VISIBLE); contentTv.setVisibility(View.VISIBLE); titleEt.setVisibility(View.INVISIBLE); contentEt.setVisibility(View.INVISIBLE); titleTv.setText(title); contentTv.setText(content); } //修改日记 else if(flag==2){ submitIb.setVisibility(View.VISIBLE); titleTv.setVisibility(View.INVISIBLE); contentTv.setVisibility(View.INVISIBLE); titleEt.setVisibility(View.VISIBLE); contentEt.setVisibility(View.VISIBLE); titleEt.setText(title); contentEt.setText(content); submitIb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(titleEt==null || titleEt.getText().toString().trim().equals("")){ Toast.makeText(DiaryInfoActivity.this, "请输入标题", Toast.LENGTH_LONG).show(); return; } if(contentEt==null || contentEt.getText().toString().trim().equals("")){ Toast.makeText(DiaryInfoActivity.this, "请输入内容", Toast.LENGTH_LONG).show(); return; } //从Sqlite中更新一条信息 DiaryDbHelper dbHelper = new DiaryDbHelper(DiaryInfoActivity.this); SQLiteDatabase db = dbHelper.getWritableDatabase(); //格式化时间 SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd HH:mm"); String date = sd.format(new Date()); ContentValues values = new ContentValues(); values.put(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE,titleEt.getText().toString()); values.put(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT,contentEt. getText().toString()); values.put(SQLiteContract.DiaryEntry.COLUMN_NAME_TIME,date); //执行更新方法 String selection = SQLiteContract.DiaryEntry._ID+"=?"; String[] selectionArgs = {id}; int count = db.update( SQLiteContract.DiaryEntry.TABLE_NAME, values, selection,selectionArgs ); DiaryInfoActivity.this.finish(); } }); } } private void setSupportActionBar(Toolbar toolbar) { } @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) { switch (item.getItemId()){ //重写 ToolBar 返回按钮的行为,关闭此 Activity case android.R.id.home: finish(); return true; }return super.onOptionsItemSelected(item); } }
//DiaryListAdapter.java package com.example.sqlitedemo; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageButton; import android.widget.TextView; import java.util.List; import java.util.Map; public class DiaryListAdapter extends BaseAdapter { private static final String TAG="DiaryListAdapter"; private Context context; private List<Map<String, String>> list; public DiaryListAdapter(Context context, List<Map<String, String>> list) { this.context = context; this.list = list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return 0; } @Override public View getView(final int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); convertView = inflater.inflate(R.layout.listview_item,null); holder = new ViewHolder(); holder.titleTv = (TextView) convertView.findViewById(R.id.title_tv); holder.timeTv = (TextView) convertView.findViewById(R.id.time_tv); holder.editIb = (ImageButton) convertView.findViewById(R.id.edit_ib); holder.deleteIb = (ImageButton) convertView.findViewById(R.id.delete_ib); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.titleTv.setText(list.get(position).get(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE)); holder.timeTv.setText(list.get(position).get(SQLiteContract.DiaryEntry.COLUMN_NAME_TIME)); holder.editIb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Intent intent = new Intent(context, DiaryInfoActivity.class); Intent intent = new Intent(context,DiaryInfoActivity.class); intent.putExtra("FLAG",2); intent.putExtra(SQLiteContract.DiaryEntry._ID,list.get(position).get(SQLiteContract.DiaryEntry._ID)); intent.putExtra(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE,list.get(position).get(SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE)); intent.putExtra(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT,list. get(position).get(SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT)); context.startActivity(intent); } }); holder.deleteIb.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder = new AlertDialog. Builder (context); builder.setTitle("提示") .setMessage("确认删除?") .setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { DiaryDbHelper dbHelper = new DiaryDbHelper(context); SQLiteDatabase db = dbHelper.getWritableDatabase(); String selection = SQLiteContract.DiaryEntry._ID+" = ?"; String[] selectionArgs = { list.get(position).get(SQLiteContract.DiaryEntry._ID) }; db.delete(SQLiteContract.DiaryEntry.TABLE_NAME,selection,selectionArgs); db.close(); list.remove(position); notifyDataSetChanged(); } }) .setNegativeButton("取消", null); builder.show(); } }); return convertView; } static class ViewHolder { TextView titleTv, timeTv; ImageButton editIb, deleteIb; } }
//DiaryDbHelper.java package com.example.sqlitedemo; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DiaryDbHelper extends SQLiteOpenHelper { public static final int DATABASE_VERSION = 1; public static final String DATABASE_NAME = "DiaryDB.db"; private static final String TEXT_TYPE = " TEXT"; private static final String DATE_TYPE = " DATE"; private static final String COMMA_SEP = ","; private static final String SQL_CREATE_ENTRIES = "CREATE TABLE " + SQLiteContract.DiaryEntry.TABLE_NAME+"("+ SQLiteContract.DiaryEntry._ID+ " " + "INTEGER PRIMARY KEY AUTOINCREMENT,"+ SQLiteContract.DiaryEntry.COLUMN_NAME_TITLE+ TEXT_TYPE+COMMA_SEP+ SQLiteContract.DiaryEntry.COLUMN_NAME_CONTENT+ TEXT_TYPE+COMMA_SEP+ SQLiteContract.DiaryEntry.COLUMN_NAME_TIME+ DATE_TYPE+")"; private static final String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS " + SQLiteContract.DiaryEntry.TABLE_NAME; public DiaryDbHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(SQL_CREATE_ENTRIES); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(SQL_DELETE_ENTRIES); onCreate(db); } }
//SQLiteContract.java package com.example.sqlitedemo; import android.provider.BaseColumns; public final class SQLiteContract { //为了防止使用者不小心实例化类的构造方法, //使构造函数私有化 private SQLiteContract() {} //此内部类定义表的内容 public static class DiaryEntry implements BaseColumns { public static final String TABLE_NAME = "diary"; public static final String COLUMN_NAME_TITLE = "title"; public static final String COLUMN_NAME_CONTENT = "content"; public static final String COLUMN_NAME_TIME = "time"; } }
//activity_login.xml <?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Space android:layout_width="match_parent" android:layout_height="197dp" /> <EditText android:id="@+id/usename_edtxt" android:layout_width="275dp" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/username" android:drawablePadding="12dp" android:gravity="center|left" android:hint="请输入用户名" android:textColorHint="@android:color/darker_gray" android:textSize="24sp" tools:ignore="MissingConstraints" /> <Space android:layout_width="match_parent" android:layout_height="46dp" /> <EditText android:id="@+id/password_edtxt" android:layout_width="272dp" android:layout_height="55dp" android:drawableLeft="@drawable/password" android:drawablePadding="12dp" android:gravity="center|left" android:hint="请输入密码" android:layout_gravity="center" android:inputType="textPassword" android:textColorHint="@android:color/darker_gray" android:textSize="24sp" tools:ignore="MissingConstraints" /> <Space android:layout_width="match_parent" android:layout_height="60dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Space android:layout_width="47dp" android:layout_height="50dp" /> <Button android:id="@+id/submit_button" android:layout_width="80dp" android:layout_height="wrap_content" android:backgroundTint="@color/white" android:text="确定" tools:ignore="MissingConstraints" /> <Space android:layout_width="150dp" android:layout_height="53dp" /> <Button android:id="@+id/register_button" android:layout_width="80dp" android:layout_height="wrap_content" android:backgroundTint="@color/white" android:text="注册" tools:ignore="DuplicateIds,MissingConstraints" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
//register.xml <?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Space android:layout_width="match_parent" android:layout_height="197dp" /> <EditText android:id="@+id/usename_edtxt" android:layout_width="275dp" android:layout_height="wrap_content" android:layout_gravity="center" android:drawableLeft="@drawable/username" android:drawablePadding="12dp" android:gravity="center|left" android:hint="请输入用户名" android:textColorHint="@android:color/darker_gray" android:textSize="24sp" tools:ignore="MissingConstraints" /> <Space android:layout_width="match_parent" android:layout_height="46dp" /> <EditText android:id="@+id/password_edtxt" android:layout_width="272dp" android:layout_height="55dp" android:drawableLeft="@drawable/password" android:drawablePadding="12dp" android:gravity="center|left" android:hint="请输入密码" android:layout_gravity="center" android:inputType="textPassword" android:textColorHint="@android:color/darker_gray" android:textSize="24sp" tools:ignore="MissingConstraints" /> <Space android:layout_width="match_parent" android:layout_height="60dp" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal"> <Space android:layout_width="150dp" android:layout_height="53dp" /> <Button android:id="@+id/register_button" android:layout_width="80dp" android:layout_height="wrap_content" android:backgroundTint="@color/white" android:text="注册" tools:ignore="DuplicateIds,MissingConstraints" /> </LinearLayout> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
五、课题实现展示
编辑编辑编辑
编辑编辑编辑
进入登录界面,输入用户名和密码,此处不能为空,此时输入的用户名没有进行注册,所以会进入注册界面显示(请先注册)或者直接点击注册按钮进入注册界面显示(进行注册)。注册输入用户名和密码点击注册按钮显示(注册成功),会返回到登录界面,输入之前注册的用户名和密码,会进入到日记本主界面,显示(登录成功)。
编辑编辑编辑
点击编辑,可以删除所写的日记内容,点击加号按钮,可添加日记内容,点击勾保存。
编辑编辑编辑
点击日记标题右边的编辑图片按钮,可进入修改界面。
编辑编辑编辑
点击日记标题一行可以查看日记内容。点击编辑可删除日记,点击×会出现删除提示。
六、总结与体会
本次上机考核,通过高级控件进行设计登录与注册界面。同时运用SQLiteOpenHelper抽象类以及契约类SQLiteContract,创建SQLiteOpenHelper的子类并覆写onCreate方法。本次考核内容基本完成,学习到数据库的使用,使我更好掌握了安卓数据库的应用,通过查找资料以及学习,完成最后先登录在跳转进入日记本的制作。通过本次实验,收获颇多,复习了以前的知识,也学习到了新的内容,对今后的学习有了很大的帮助。
下载资源包:
https://download.csdn.net/download/weixin_48388330/76315152
资源中的图片以及内容只适用与学习