android studio项目实战——备忘录(附源码)

宁睿  金牌会员 | 2024-6-19 22:25:53 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 570|帖子 570|积分 1718

成果展示:

 1.前期准备

(1)在配置文件中添加权限及启动页面次序

①展开工程,打开app下方的AndroidManifest.xml,添加权限,如下:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


② 依旧在AndroidManifest.xml文件中添加启动页面次序的功能代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.     package="com.example.ts_menu">        //这里注意修改成自己创建的包名
  4.     <uses-permission android:name="android.permission.CAMERA" />
  5.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  6.     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
  7.     <application
  8.         android:allowBackup="true"
  9.         android:icon="@mipmap/ic_launcher"
  10.         android:label="@string/app_name"
  11.         android:roundIcon="@mipmap/ic_launcher_round"
  12.         android:supportsRtl="true"
  13.         android:theme="@style/AppTheme">
  14.         <activity android:name=".AddInfoActivity"></activity>
  15.         <activity android:name=".LoginActivity">
  16.             <intent-filter>
  17.                 <action android:name="android.intent.action.MAIN" />
  18.                 <category android:name="android.intent.category.LAUNCHER" />
  19.             </intent-filter>
  20.         </activity>
  21.         <activity android:name=".MainActivity">
  22.         </activity>
  23.     </application>
  24. </manifest>
复制代码
(2)添加依靠

展开工程,打开app下方的build.gradle ,添加依靠,如下:依靠添加好之后,要记取同步,在页面右上角的位置单击:Sync Now  即可。

implementation 'com.android.support:recyclerview-v7:+'
implementation 'com.github.bumptech.glide:glide:4.9.0'
api 'com.blankj:utilcode:1.23.7'


(3)素材

一共5张图片,粘贴到工程的drawable文件夹下来,此中bgone.png,bgthree.jpg两个图片是登录界面和信息添加界面的背景,buttonbg.png,savebg.png图片是添加备忘录按钮和保存按钮的背景,别的一张背景图片是sunshine.jpg是一张默认表现的照片。选择你本身喜好的图片添加进去吧!
2.所需的布局文件


 2.1 activity_login.xml


登录布局界面的实现
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     android:background="@drawable/bgone"
  9.     tools:context=".LoginActivity">
  10.     <TextView
  11.         android:layout_width="match_parent"
  12.         android:layout_height="wrap_content"
  13.         android:text="用户登录"
  14.         android:textStyle="bold"
  15.         android:textColor="#000000"
  16.         android:textSize="40sp"
  17.         android:layout_margin="100dp"
  18.         android:gravity="center"/>
  19.     <LinearLayout
  20.         android:layout_width="match_parent"
  21.         android:layout_height="wrap_content"
  22.         android:orientation="horizontal"
  23.         android:layout_margin="10dp">
  24.         <TextView
  25.             android:layout_width="wrap_content"
  26.             android:layout_height="wrap_content"
  27.             android:text="用户名:"
  28.             android:textColor="#000000"
  29.             android:textSize="30sp"        />
  30.         <EditText
  31.             android:id="@+id/editText_inputname"
  32.             android:layout_width="match_parent"
  33.             android:layout_height="wrap_content"
  34.             android:hint="请输入用户名"
  35.             android:textColor="#000000"
  36.             android:textSize="30sp"        />
  37.     </LinearLayout>
  38.     <LinearLayout
  39.         android:layout_width="match_parent"
  40.         android:layout_height="wrap_content"
  41.         android:orientation="horizontal"
  42.         android:layout_margin="10dp">
  43.         <TextView
  44.             android:layout_width="wrap_content"
  45.             android:layout_height="wrap_content"
  46.             android:text="密    码:"
  47.             android:textColor="#000000"
  48.             android:textSize="30sp"        />
  49.         <EditText
  50.             android:id="@+id/editText_inputpwd"
  51.             android:layout_width="match_parent"
  52.             android:layout_height="wrap_content"
  53.             android:hint="请输入密码"
  54.             android:textColor="#000000"
  55.             android:inputType="textPassword"
  56.             android:textSize="30sp"        />
  57.     </LinearLayout>
  58.     <CheckBox
  59.         android:id="@+id/checkBox_reme"
  60.         android:layout_width="wrap_content"
  61.         android:layout_height="wrap_content"
  62.         android:text="记住密码"
  63.         android:layout_gravity="right"
  64.         android:layout_margin="10dp"/>
  65.     <LinearLayout
  66.         android:layout_width="match_parent"
  67.         android:layout_height="wrap_content"
  68.         android:orientation="horizontal"
  69.         android:layout_margin="10dp">
  70.         <Button
  71.             android:layout_width="wrap_content"
  72.             android:layout_height="wrap_content"
  73.             android:text="取消"
  74.             android:textColor="#000000"
  75.             android:textSize="30sp"
  76.             android:layout_weight="1"/>
  77.         <Button
  78.             android:id="@+id/button_login"
  79.             android:layout_width="wrap_content"
  80.             android:layout_height="wrap_content"
  81.             android:text="登录"
  82.             android:textColor="#000000"
  83.             android:textSize="30sp"
  84.             android:layout_weight="1"/>
  85.     </LinearLayout>
  86. </LinearLayout>
复制代码
2.2 activity_main.xml文件代码:

添加备忘录界面
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     tools:context=".MainActivity">
  9.     <TextView
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:text="愿这小小的备忘录,记下我生活中的点点滴滴"
  13.         android:textStyle="bold"
  14.         android:textColor="#000000"
  15.         android:layout_gravity="center"
  16.         android:layout_margin="10dp"        />
  17.     <Button
  18.         android:id="@+id/button_add"
  19.         android:layout_width="wrap_content"
  20.         android:layout_height="wrap_content"
  21.         android:text="添加备忘录"
  22.         android:textStyle="bold"
  23.         android:textColor="#000000"
  24.         android:layout_gravity="right"
  25.         android:background="@drawable/buttonbg"
  26.         android:layout_margin="10dp"        />
  27.     <android.support.v7.widget.RecyclerView
  28.         android:id="@+id/recy_view"
  29.         android:layout_width="match_parent"
  30.         android:layout_height="wrap_content"/>
  31. </LinearLayout>
复制代码
 2.3 activity_add_info.xml文件代码:

备忘录信息添加布局界面
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:orientation="vertical"
  8.     android:background="@drawable/bgthree"
  9.     tools:context=".AddInfoActivity">
  10.     <EditText
  11.         android:id="@+id/editText_title"
  12.         android:layout_width="match_parent"
  13.         android:layout_height="wrap_content"
  14.         android:hint="备忘录的标题"
  15.         android:textStyle="bold"
  16.         android:textColor="#000000"
  17.         android:textSize="30sp"/>
  18.     <View
  19.         android:layout_width="match_parent"
  20.         android:layout_height="2dp"
  21.         android:background="#009688"
  22.         android:layout_marginTop="-10dp"/>
  23.     <EditText
  24.         android:id="@+id/editText_content"
  25.         android:layout_width="match_parent"
  26.         android:layout_height="200dp"
  27.         android:hint="备忘录的内容"
  28.         android:textStyle="bold"
  29.         android:textColor="#000000"
  30.         android:textSize="20sp"
  31.         android:gravity="top"/>
  32.     <View
  33.         android:layout_width="match_parent"
  34.         android:layout_height="2dp"
  35.         android:background="#009688"
  36.         android:layout_marginTop="-10dp"/>
  37.     <TextView
  38.         android:layout_width="match_parent"
  39.         android:layout_height="wrap_content"
  40.         android:text="请选用下面的任一种方式,添加一张心情图片:"
  41.         android:textStyle="bold"
  42.         android:textColor="#000000"
  43.         android:textSize="15sp"
  44.         android:gravity="top"
  45.         android:layout_margin="10dp"/>
  46.     <LinearLayout
  47.         android:layout_width="match_parent"
  48.         android:layout_height="wrap_content"
  49.         android:orientation="horizontal"
  50.         android:layout_margin="10dp">
  51.         <Button
  52.             android:id="@+id/button_camera"
  53.             android:layout_width="wrap_content"
  54.             android:layout_height="wrap_content"
  55.             android:text="拍照"
  56.             android:textStyle="bold"
  57.             android:textColor="#000000"
  58.             android:textSize="15sp"
  59.             android:layout_margin="10dp"/>
  60.         <Button
  61.             android:id="@+id/button_photo"
  62.             android:layout_width="wrap_content"
  63.             android:layout_height="wrap_content"
  64.             android:text="从图库中选择"
  65.             android:textStyle="bold"
  66.             android:textColor="#000000"
  67.             android:textSize="15sp"
  68.             android:layout_margin="10dp"/>
  69.     </LinearLayout>
  70.     <ImageView
  71.         android:id="@+id/imageView_preview"
  72.         android:layout_width="wrap_content"
  73.         android:layout_height="200dp"
  74.         android:src="@drawable/sunshine"
  75.         android:layout_marginBottom="20dp"
  76.         android:layout_gravity="center"/>
  77.     <Button
  78.         android:id="@+id/button_save"
  79.         android:layout_width="match_parent"
  80.         android:layout_height="wrap_content"
  81.         android:text="保存"
  82.         android:textStyle="bold"
  83.         android:textColor="#000000"
  84.         android:textSize="30sp"
  85.         android:background="@drawable/savebg"
  86.         android:layout_margin="10dp"/>
  87. </LinearLayout>
复制代码
 2.4 recy_item.xml文件代码:

主界面--子布局界面
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="horizontal"
  4.     android:layout_width="match_parent"
  5.     android:layout_height="wrap_content"
  6.     android:background="#7AECCC"
  7.     android:id="@+id/item_layout"
  8.     android:layout_margin="5dp"    >
  9.     <LinearLayout
  10.         android:layout_width="wrap_content"
  11.         android:layout_height="wrap_content"
  12.         android:orientation="vertical"
  13.         android:layout_gravity="center"
  14.         android:layout_weight="1"
  15.         android:layout_margin="5dp"
  16.         >
  17.         <TextView
  18.             android:id="@+id/item_title"
  19.             android:layout_width="match_parent"
  20.             android:layout_height="wrap_content"
  21.             android:text="标题"
  22.             android:textSize="20sp"
  23.             android:textStyle="bold"
  24.             android:textColor="#000000"/>
  25.         <TextView
  26.             android:id="@+id/item_content"
  27.             android:layout_width="wrap_content"
  28.             android:layout_height="wrap_content"
  29.             android:text="内容"
  30.             android:textColor="#000000"/>
  31.     </LinearLayout>
  32.     <LinearLayout
  33.         android:layout_width="wrap_content"
  34.         android:layout_height="wrap_content"
  35.         android:orientation="vertical"
  36.         android:layout_margin="5dp">
  37.         <ImageView
  38.             android:id="@+id/item_image"
  39.             android:layout_width="100dp"
  40.             android:layout_height="100dp"
  41.             android:src="@mipmap/ic_launcher_round"/>
  42.         <TextView
  43.             android:id="@+id/item_time"
  44.             android:layout_width="wrap_content"
  45.             android:layout_height="wrap_content"
  46.             android:text="时间"
  47.             android:textColor="#000000"
  48.             android:layout_gravity="center"/>
  49.     </LinearLayout>
  50. </LinearLayout>
复制代码

3.所需的java类文件

以下是所需要添加的package,及java类文件。
package所需要添加的文件有adapter、bean、db三个package包。
java类文件除了开始的主文件MainActivity,还需添加MemoAdapter、MemoBean、MydbHelper、AddInfoActivity、LoginActivity5个java类文件。

 3.1 MemoAdapter文件代码:

备忘录的自界说适配器功能代码
  1. package com.example.ts_menu.adapter;
  2. import android.app.AlertDialog;
  3. import android.content.Context;
  4. import android.content.DialogInterface;
  5. import android.database.sqlite.SQLiteDatabase;
  6. import android.graphics.Color;
  7. import android.graphics.drawable.GradientDrawable;
  8. import android.os.Build;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.widget.ImageView;
  13. import android.widget.LinearLayout;
  14. import android.widget.TextView;
  15. import androidx.annotation.NonNull;
  16. import androidx.annotation.RequiresApi;
  17. import androidx.recyclerview.widget.RecyclerView;
  18. import com.bumptech.glide.Glide;
  19. import com.example.ts_menu.R;
  20. import com.example.ts_menu.bean.MemoBean;
  21. import com.example.ts_menu.db.MyDbHelper;
  22. import java.util.List;
  23. import java.util.Random;
  24. //1 类文件后面添加泛型
  25. //2 鼠标定位类文件行红色波浪线处,Alt+Enter键:添加未实现的方法
  26. //3 鼠标定位类文件行ViewHolder处,Alt+Enter键:添加内部类
  27. //4 鼠标定位界面最下方内部类ViewHolder处,添加extends  RecyclerView.ViewHolder
  28. //5 鼠标定位界面最下方内部类ViewHolder红色波浪线处,Alt+Enter键:添加构造方法
  29. //6 定义两个对象:上下文环境和数组
  30. //7 定义两个对象下方的空白处:Alt+Insert键,添加适配器的构造方法
  31. public class MemoAdapter extends RecyclerView.Adapter<MemoAdapter.ViewHolder>  {
  32.     private Context mcontext;
  33.     private List<MemoBean> arr1;
  34.     private MyDbHelper mhelper1;
  35.     private SQLiteDatabase db;
  36.     public MemoAdapter(Context mcontext, List<MemoBean> arr1) {
  37.         this.mcontext = mcontext;
  38.         this.arr1 = arr1;
  39.     }
  40.     //负责加载item布局
  41.     @NonNull
  42.     @Override
  43.     public MemoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
  44.         View view= LayoutInflater.from(mcontext).inflate(R.layout.recy_item,parent,false);
  45.         ViewHolder mholder=new ViewHolder(view);
  46.         return mholder;
  47.     }
  48.     //负责加载item的数据
  49.     @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
  50.     @Override
  51.     public void onBindViewHolder(@NonNull MemoAdapter.ViewHolder mholder, final int i) {
  52.         final MemoBean memoBean=arr1.get(i);
  53.         mholder.item_title.setText(memoBean.getTitle());
  54.         mholder.item_content.setText(memoBean.getContent());
  55.         mholder.item_time.setText(memoBean.getTime());
  56.         Glide.with(mcontext).load(memoBean.getImgpath()).into(mholder.item_img);
  57.         // 完善:设置RecyclerView中每一个子项的颜色和形状
  58.         Random random = new Random();
  59.         int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
  60.         GradientDrawable gradientDrawable = new GradientDrawable();
  61.         gradientDrawable.setShape(GradientDrawable.RECTANGLE);//形状
  62.         gradientDrawable.setCornerRadius(10f);//设置圆角Radius
  63.         gradientDrawable.setColor(color);//颜色
  64.         mholder.item_layout.setBackground(gradientDrawable);//设置为background
  65.         //完善:单击其中的一个子项,弹出删除功能
  66.         mholder.item_layout.setOnClickListener(new View.OnClickListener() {
  67.             @Override
  68.             public void onClick(View view) {
  69.                 //弹出对话框,删除
  70.                 AlertDialog.Builder dialog=new AlertDialog.Builder(mcontext);
  71.                 dialog.setMessage("确定删除吗?");
  72.                 dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
  73.                     @Override
  74.                     public void onClick(DialogInterface dialogInterface, int abc) {
  75.                         //从数据库当中删除掉
  76.                         mhelper1= new MyDbHelper(mcontext);
  77.                         db=mhelper1.getWritableDatabase();
  78.                         db.delete("tb_memory","title=?",new String[]{arr1.get(i).getTitle()});
  79.                         arr1.remove(i);
  80.                         notifyItemRemoved(i);
  81.                         dialogInterface.dismiss();
  82.                     }
  83.                 });
  84.                 dialog.setNegativeButton("取消",null);
  85.                 dialog.setCancelable(false);
  86.                 dialog.create();
  87.                 dialog.show();
  88.             }
  89.         });
  90.     }
  91.     //recyView一共有多少个子项
  92.     @Override
  93.     public int getItemCount() {
  94.         return arr1.size();
  95.     }
  96.     public class ViewHolder  extends  RecyclerView.ViewHolder{
  97.         TextView item_title,item_content,item_time;
  98.         ImageView item_img;
  99.         LinearLayout item_layout;
  100.         public ViewHolder(@NonNull View itemView) {
  101.             super(itemView);
  102.             item_title=itemView.findViewById(R.id.item_title);
  103.             item_content=itemView.findViewById(R.id.item_content);
  104.             item_img=itemView.findViewById(R.id.item_image);
  105.             item_time=itemView.findViewById(R.id.item_time);
  106.             item_layout=itemView.findViewById(R.id.item_layout);
  107.         }
  108.     }
  109. }
复制代码
3.2 MemoBean文件代码: 

一个javabean文件,为了存储备忘录的信息
  1. package com.example.ts_menu.bean;
  2. public class MemoBean {
  3.         private  String title;
  4.         private String content;
  5.         private String imgpath;
  6.         private String time;
  7.         public MemoBean(String title, String content, String imgpath, String time) {
  8.             this.title = title;
  9.             this.content = content;
  10.             this.imgpath = imgpath;
  11.             this.time = time;
  12.         }
  13.         public String getTitle() {
  14.             return title;
  15.         }
  16.         public void setTitle(String title) {
  17.             this.title = title;
  18.         }
  19.         public String getContent() {
  20.             return content;
  21.         }
  22.         public void setContent(String content) {
  23.             this.content = content;
  24.         }
  25.         public String getImgpath() {
  26.             return imgpath;
  27.         }
  28.         public void setImgpath(String imgpath) {
  29.             this.imgpath = imgpath;
  30.         }
  31.         public String getTime() {
  32.             return time;
  33.         }
  34.         public void setTime(String time) {
  35.             this.time = time;
  36.         }
  37.     }
复制代码
3.3 MydbHelper文件代码: 

数据库文件
  1. package com.example.ts_menu.db;
  2. import android.content.Context;
  3. import android.database.sqlite.SQLiteDatabase;
  4. import android.database.sqlite.SQLiteOpenHelper;
  5. public class MyDbHelper extends SQLiteOpenHelper {
  6.     private  static String DBNAME="zsmemo.db";
  7.     private  static int VERSION=1;
  8.     //构造方法
  9.     public MyDbHelper( Context context) {
  10.         super(context, DBNAME, null, VERSION);
  11.     }
  12.     // 创建数据库
  13.     @Override
  14.     public void onCreate(SQLiteDatabase db) {
  15.         //创建数据表
  16.         db.execSQL("create table tb_memory(_id Integer primary key,title String (200),content String (2000),imgpath String (200),mtime String (50))");
  17.     }
  18.     //升级数据库
  19.     @Override
  20.     public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
  21.     }
  22. }
复制代码
3.4 AddInfoActivity文件代码: 

 拍照功能直接闪退:
  1. if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
  2. {
  3.     StrictMode.VmPolicy.Builder builder=new StrictMode.VmPolicy.Builder();
  4.     StrictMode.setVmPolicy(builder.build());
  5. }
复制代码
利用上述代码防止闪退。
在拍照功能时应该将保存路径代码改为tmp_path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/image"+randtime+".jpg";
有用防止拍照后确定不了问题。
  1. package com.example.ts_menu;
  2. import android.content.ContentValues;
  3. import android.content.Intent;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.net.Uri;
  6. import android.os.Environment;
  7. import android.provider.MediaStore;
  8. import android.os.Bundle;
  9. import android.text.format.Time;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.ImageView;
  14. import android.widget.Toast;
  15. import androidx.annotation.Nullable;
  16. import androidx.appcompat.app.AppCompatActivity;
  17. import com.blankj.utilcode.util.UriUtils;
  18. import com.bumptech.glide.Glide;
  19. import com.example.ts_menu.db.MyDbHelper;
  20. import com.example.ts_menu.db.MyDbHelper;
  21. import java.io.File;
  22. import java.io.IOException;
  23. public class AddInfoActivity extends AppCompatActivity {
  24.     //定义对象
  25.     EditText edit_title,edit_content;
  26.     Button btn_camera,btn_photo,btn_save;
  27.     ImageView img_preview;
  28.     String tmp_path,disp_path;
  29.     MyDbHelper mhelper;
  30.     SQLiteDatabase db;
  31.     @Override
  32.     protected void onCreate(Bundle savedInstanceState) {
  33.         super.onCreate(savedInstanceState);
  34.         setContentView(R.layout.activity_add_info);
  35.         //1 绑定控件
  36.         initView();
  37.         //2 单击按钮、拍照、从图库中选择照片
  38.         btnOnClick();
  39.         //3 接受拍好照片、接受从图库当中选择的照片 ------方法:系统回调
  40.         //4 把信息保存到数据库中
  41.         btnSave();
  42.     }
  43.     //1 绑定控件-----代码
  44.     private void initView() {
  45.         edit_title=findViewById(R.id.editText_title);
  46.         edit_content=findViewById(R.id.editText_content);
  47.         btn_camera=findViewById(R.id.button_camera);
  48.         btn_photo=findViewById(R.id.button_photo);
  49.         img_preview=findViewById(R.id.imageView_preview);
  50.         btn_save=findViewById(R.id.button_save);
  51.         mhelper=new MyDbHelper(AddInfoActivity.this);
  52.         db= mhelper.getWritableDatabase();
  53.     }
  54.     //2 单击按钮、拍照----------代码
  55.     private void btnOnClick() {
  56.         btn_camera.setOnClickListener(new View.OnClickListener() {
  57.             @Override
  58.             public void onClick(View view) {
  59.                 //拍照
  60.                 Time time=new Time();
  61.                 time.setToNow();
  62.                 String randtime=time.year+(time.month+1)+time.monthDay+time.hour+time.minute+time.second+"";
  63.                // tmp_path= Environment.getExternalStorageDirectory()+"/image"+ randtime+".jpg";
  64.                   tmp_path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/image"+randtime+".jpg";
  65.                 File imgfile=new File(tmp_path);
  66.                 try {
  67.                     imgfile.createNewFile();
  68.                 } catch (IOException e) {
  69.                     e.printStackTrace();
  70.                 }
  71.                 Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
  72.                 intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgfile) );
  73.                 startActivityForResult(intent,11);
  74.             }
  75.         });
  76.         btn_photo.setOnClickListener(new View.OnClickListener() {
  77.             @Override
  78.             public void onClick(View view) {
  79.                 //选择照片
  80.                 Intent intent=new Intent("android.intent.action.GET_CONTENT");
  81.                 intent.setType("image/*");
  82.                 startActivityForResult(intent,22);
  83.             }
  84.         });
  85.     }
  86.     //3 接受拍好照片、接受从图库当中选择的照片 ------方法:系统回调
  87.     @Override
  88.     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  89.         switch (requestCode){
  90.             case 11:
  91.                 if(resultCode==RESULT_OK){
  92.                     disp_path=tmp_path;
  93.                     Glide.with(AddInfoActivity.this).load(disp_path).into(img_preview);
  94.                 }
  95.                 break;
  96.             case 22:
  97.                 Uri imageuri=data.getData();
  98.                 if (imageuri==null)
  99.                 {
  100.                     return;
  101.                 }
  102.                 disp_path=UriUtils.uri2File(imageuri).getPath();
  103.                 Glide.with(AddInfoActivity.this).load(disp_path).into(img_preview);
  104.                 break;
  105.             default:
  106.                 break;
  107.         }
  108.     }
  109.     //4 把信息保存到数据库中-------------代码
  110.     private void btnSave() {
  111.         btn_save.setOnClickListener(new View.OnClickListener() {
  112.             @Override
  113.             public void onClick(View view) {
  114.                 //保存信息到数据库代码
  115.                 Time time=new Time();
  116.                 time.setToNow();
  117.                 ContentValues contentValues=new ContentValues();//一行
  118.                 contentValues.put("title",edit_title.getText().toString());//1行——1列
  119.                 contentValues.put("content",edit_content.getText().toString());//1行——3列
  120.                 contentValues.put("imgpath",disp_path);
  121.                 contentValues.put("mtime",time.year+"/"+(time.month+1)+"/"+time.monthDay);
  122.                 db.insert("tb_memory",null,contentValues);
  123.                 Toast.makeText(AddInfoActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
  124.                 //跳转到主界面
  125.                 Intent intent=new Intent(AddInfoActivity.this,MainActivity.class);
  126.                 startActivity(intent);
  127.                 finish();
  128.             }
  129.         });
  130.     }
  131. }
复制代码
3.5 LoginActivity文件代码:

  1. package com.example.ts_menu;
  2. import android.content.Intent;
  3. import android.content.SharedPreferences;
  4. //import android.support.v7.app.AppCompatActivity;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.CheckBox;
  9. import android.widget.EditText;
  10. import androidx.appcompat.app.AppCompatActivity;
  11. public class LoginActivity extends AppCompatActivity {
  12.     //定义对象
  13.     private EditText edit_inputname,edit_inputpwd;
  14.     private CheckBox check_reme;
  15.     private Button btn_login;
  16.     @Override
  17.     protected void onCreate(Bundle savedInstanceState) {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_login);
  20.         //1 绑定控件
  21.         initView();
  22.         //2 单击登录按钮,将用户名和密码保存起来
  23.         btnloginonClick();
  24.         //3 下次启动,直接显示用户名和密码
  25.         displayinfo();
  26.     }
  27.     //1 绑定控件--------代码
  28.     private void initView() {
  29.         edit_inputname=findViewById(R.id.editText_inputname);
  30.         edit_inputpwd=findViewById(R.id.editText_inputpwd);
  31.         check_reme=findViewById(R.id.checkBox_reme);
  32.         btn_login=findViewById(R.id.button_login);
  33.     }
  34.     //2 单击登录按钮,将用户名和密码保存起来----代码
  35.     private void btnloginonClick() {
  36.         btn_login.setOnClickListener(new View.OnClickListener() {
  37.             @Override
  38.             public void onClick(View view) {
  39.                 //保存用户名和密码
  40.                 SharedPreferences.Editor editor=getSharedPreferences("myinfo",0).edit();
  41.                 editor.putString("name",edit_inputname.getText().toString());
  42.                 editor.putString("pwd",edit_inputpwd.getText().toString());
  43.                 editor.putBoolean("st",check_reme.isChecked());
  44.                 editor.commit();
  45.                 //跳转到第二页
  46.                 Intent intent=new Intent(LoginActivity.this,MainActivity.class);
  47.                 startActivity(intent);
  48.             }
  49.         });
  50.     }
  51.     //3 下次启动,直接显示用户名和密码-----代码
  52.     private void displayinfo() {
  53.         String strname=getSharedPreferences("myinfo",0).getString("name","");
  54.         String strpwd=getSharedPreferences("myinfo",0).getString("pwd","");
  55.         Boolean status=getSharedPreferences("myinfo",0).getBoolean("st",false);
  56.         if(status==true){
  57.             edit_inputname.setText(strname);
  58.             edit_inputpwd.setText(strpwd);
  59.             check_reme.setChecked(true);
  60.         }else{
  61.             edit_inputname.setText("");
  62.             edit_inputpwd.setText("");
  63.             check_reme.setChecked(false);
  64.         }
  65.     }
  66. }
复制代码
3.6 MainActivity文件代码:

  1. package com.example.ts_menu;
  2. import android.content.Intent;
  3. import android.database.Cursor;
  4. import android.database.sqlite.SQLiteDatabase;
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.Adapter;
  8. import android.widget.Button;
  9. import androidx.appcompat.app.AppCompatActivity;
  10. import androidx.recyclerview.widget.RecyclerView;
  11. import androidx.recyclerview.widget.StaggeredGridLayoutManager;
  12. import com.example.ts_menu.adapter.MemoAdapter;
  13. import com.example.ts_menu.bean.MemoBean;
  14. import com.example.ts_menu.db.MyDbHelper;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. public class MainActivity extends AppCompatActivity {
  18.     //定义对象
  19.     private Button btn_add;
  20.     private RecyclerView recy_view;
  21.     private MyDbHelper mhelper;
  22.     SQLiteDatabase db;
  23.     @Override
  24.     protected void onCreate(Bundle savedInstanceState) {
  25.         super.onCreate(savedInstanceState);
  26.         setContentView(R.layout.activity_main);
  27.         //1 绑定控件
  28.         initView();
  29.         //2 对单击添加单击事件
  30.         btnonclicknext();
  31.         //3完善:从数据库获取数据,显示到RecyclerView控件里面
  32.         recyDisplay();
  33.     }
  34.     //1 绑定控件-----------代码
  35.     private void initView() {
  36.         btn_add=findViewById(R.id.button_add);
  37.         recy_view=findViewById(R.id.recy_view);
  38.         mhelper=new MyDbHelper(MainActivity.this);
  39.         db=mhelper.getWritableDatabase();
  40.     }
  41.     //2 对单击添加单击事件-----代码
  42.     private void btnonclicknext() {
  43.         btn_add.setOnClickListener(new View.OnClickListener() {
  44.             @Override
  45.             public void onClick(View view) {
  46.                 //单击后跳转到一下页
  47.                 Intent intent=new Intent(MainActivity.this,AddInfoActivity.class);
  48.                 startActivity(intent);
  49.                 finish();
  50.             }
  51.         });
  52.     }
  53.     //3完善:从数据库获取数据,显示到RecyclerView控件里面---------------代码
  54.     private void recyDisplay() {
  55.         //3.1准备数据-----------标题、内容、图片、时间(类)
  56.         List<MemoBean> arr=new ArrayList();
  57.         //从数据库里面取数据了
  58.         Cursor  cursor=db.rawQuery("select * from tb_memory",null);
  59.         while(cursor.moveToNext()){
  60.             String mytitle=cursor.getString(cursor.getColumnIndex("title"));
  61.             String mycontent=cursor.getString(cursor.getColumnIndex("content"));
  62.             String myimgpath=cursor.getString(cursor.getColumnIndex("imgpath"));
  63.             String mymtime=cursor.getString(cursor.getColumnIndex("mtime"));
  64.             MemoBean memoBean=new MemoBean(mytitle,mycontent,myimgpath,mymtime);
  65.             arr.add(memoBean);
  66.         }
  67.         cursor.close();
  68.         //3.2 子布局 recy_item
  69.         //3.3 数据------桥(适配器MemoAdapter)----------------子布局
  70.         MemoAdapter adapter=new MemoAdapter(MainActivity.this,arr);
  71.         //3.4 确定显示的方式
  72.         StaggeredGridLayoutManager st=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
  73.         recy_view.setLayoutManager(st);
  74.         //3.5 让数据显示出来
  75.         recy_view.setAdapter(adapter);
  76.     }
  77. }
复制代码
留意:activity文件中相关的名称报错,得换成本身所创建工程的名字。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

宁睿

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表