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

news2024/10/5 16:27:25

成果展示:

 1.前期准备

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

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

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

② 依旧在AndroidManifest.xml文件中添加启动页面顺序的功能代码

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.ts_menu">        //这里注意修改成自己创建的包名

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

    <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=".AddInfoActivity"></activity>
        <activity android:name=".LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity">
        </activity>
    </application>

</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

登录布局界面的实现

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="@drawable/bgone"
    tools:context=".LoginActivity">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="用户登录"
        android:textStyle="bold"
        android:textColor="#000000"
        android:textSize="40sp"
        android:layout_margin="100dp"
        android:gravity="center"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:"
            android:textColor="#000000"
            android:textSize="30sp"        />
        <EditText
            android:id="@+id/editText_inputname"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"
            android:textColor="#000000"
            android:textSize="30sp"        />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密    码:"
            android:textColor="#000000"
            android:textSize="30sp"        />
        <EditText
            android:id="@+id/editText_inputpwd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入密码"
            android:textColor="#000000"
            android:inputType="textPassword"
            android:textSize="30sp"        />
    </LinearLayout>

    <CheckBox
        android:id="@+id/checkBox_reme"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="记住密码"
        android:layout_gravity="right"
        android:layout_margin="10dp"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="取消"
            android:textColor="#000000"
            android:textSize="30sp"
            android:layout_weight="1"/>
        <Button
            android:id="@+id/button_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textColor="#000000"
            android:textSize="30sp"
            android:layout_weight="1"/>
    </LinearLayout>
</LinearLayout>

2.2 activity_main.xml文件代码:

添加备忘录界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="愿这小小的备忘录,记下我生活中的点点滴滴"
        android:textStyle="bold"
        android:textColor="#000000"
        android:layout_gravity="center"
        android:layout_margin="10dp"        />
    <Button
        android:id="@+id/button_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="添加备忘录"
        android:textStyle="bold"
        android:textColor="#000000"
        android:layout_gravity="right"
        android:background="@drawable/buttonbg"
        android:layout_margin="10dp"        />
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recy_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

 2.3 activity_add_info.xml文件代码:

备忘录信息添加布局界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    android:background="@drawable/bgthree"
    tools:context=".AddInfoActivity">
    <EditText
        android:id="@+id/editText_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="备忘录的标题"
        android:textStyle="bold"
        android:textColor="#000000"
        android:textSize="30sp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#009688"
        android:layout_marginTop="-10dp"/>
    <EditText
        android:id="@+id/editText_content"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:hint="备忘录的内容"
        android:textStyle="bold"
        android:textColor="#000000"
        android:textSize="20sp"
        android:gravity="top"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#009688"
        android:layout_marginTop="-10dp"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="请选用下面的任一种方式,添加一张心情图片:"
        android:textStyle="bold"
        android:textColor="#000000"
        android:textSize="15sp"
        android:gravity="top"
        android:layout_margin="10dp"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_margin="10dp">
        <Button
            android:id="@+id/button_camera"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="拍照"
            android:textStyle="bold"
            android:textColor="#000000"
            android:textSize="15sp"
            android:layout_margin="10dp"/>
        <Button
            android:id="@+id/button_photo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="从图库中选择"
            android:textStyle="bold"
            android:textColor="#000000"
            android:textSize="15sp"
            android:layout_margin="10dp"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/imageView_preview"
        android:layout_width="wrap_content"
        android:layout_height="200dp"
        android:src="@drawable/sunshine"
        android:layout_marginBottom="20dp"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/button_save"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="保存"
        android:textStyle="bold"
        android:textColor="#000000"
        android:textSize="30sp"
        android:background="@drawable/savebg"
        android:layout_margin="10dp"/>
</LinearLayout>

 2.4 recy_item.xml文件代码:

主界面--子布局界面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#7AECCC"
    android:id="@+id/item_layout"
    android:layout_margin="5dp"    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_gravity="center"
        android:layout_weight="1"
        android:layout_margin="5dp"
        >
        <TextView
            android:id="@+id/item_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="标题"
            android:textSize="20sp"
            android:textStyle="bold"
            android:textColor="#000000"/>
        <TextView
            android:id="@+id/item_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="内容"
            android:textColor="#000000"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_margin="5dp">
        <ImageView
            android:id="@+id/item_image"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:src="@mipmap/ic_launcher_round"/>
        <TextView
            android:id="@+id/item_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="时间"
            android:textColor="#000000"
            android:layout_gravity="center"/>
    </LinearLayout>

</LinearLayout>

3.所需的java类文件

以下是所需要添加的package,及java类文件。

package所需要添加的文件有adapter、bean、db三个package包。

java类文件除了开始的主文件MainActivity,还需添加MemoAdapter、MemoBean、MydbHelper、AddInfoActivity、LoginActivity5个java类文件。

 3.1 MemoAdapter文件代码:

备忘录的自定义适配器功能代码

package com.example.ts_menu.adapter;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.ts_menu.R;
import com.example.ts_menu.bean.MemoBean;
import com.example.ts_menu.db.MyDbHelper;


import java.util.List;
import java.util.Random;
//1 类文件后面添加泛型
//2 鼠标定位类文件行红色波浪线处,Alt+Enter键:添加未实现的方法
//3 鼠标定位类文件行ViewHolder处,Alt+Enter键:添加内部类
//4 鼠标定位界面最下方内部类ViewHolder处,添加extends  RecyclerView.ViewHolder
//5 鼠标定位界面最下方内部类ViewHolder红色波浪线处,Alt+Enter键:添加构造方法
//6 定义两个对象:上下文环境和数组
//7 定义两个对象下方的空白处:Alt+Insert键,添加适配器的构造方法

public class MemoAdapter extends RecyclerView.Adapter<MemoAdapter.ViewHolder>  {
    private Context mcontext;
    private List<MemoBean> arr1;
    private MyDbHelper mhelper1;
    private SQLiteDatabase db;

    public MemoAdapter(Context mcontext, List<MemoBean> arr1) {
        this.mcontext = mcontext;
        this.arr1 = arr1;
    }

    //负责加载item布局
    @NonNull
    @Override
    public MemoAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        View view= LayoutInflater.from(mcontext).inflate(R.layout.recy_item,parent,false);
        ViewHolder mholder=new ViewHolder(view);
        return mholder;
    }


    //负责加载item的数据
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindViewHolder(@NonNull MemoAdapter.ViewHolder mholder, final int i) {
        final MemoBean memoBean=arr1.get(i);
        mholder.item_title.setText(memoBean.getTitle());
        mholder.item_content.setText(memoBean.getContent());
        mholder.item_time.setText(memoBean.getTime());
        Glide.with(mcontext).load(memoBean.getImgpath()).into(mholder.item_img);

        // 完善:设置RecyclerView中每一个子项的颜色和形状
        Random random = new Random();
        int color = Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256));
        GradientDrawable gradientDrawable = new GradientDrawable();
        gradientDrawable.setShape(GradientDrawable.RECTANGLE);//形状
        gradientDrawable.setCornerRadius(10f);//设置圆角Radius
        gradientDrawable.setColor(color);//颜色
        mholder.item_layout.setBackground(gradientDrawable);//设置为background

        //完善:单击其中的一个子项,弹出删除功能
        mholder.item_layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //弹出对话框,删除
                AlertDialog.Builder dialog=new AlertDialog.Builder(mcontext);
                dialog.setMessage("确定删除吗?");
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int abc) {
                        //从数据库当中删除掉
                        mhelper1= new MyDbHelper(mcontext);
                        db=mhelper1.getWritableDatabase();
                        db.delete("tb_memory","title=?",new String[]{arr1.get(i).getTitle()});
                        arr1.remove(i);
                        notifyItemRemoved(i);
                        dialogInterface.dismiss();

                    }
                });
                dialog.setNegativeButton("取消",null);
                dialog.setCancelable(false);
                dialog.create();
                dialog.show();

            }
        });

    }

    //recyView一共有多少个子项
    @Override
    public int getItemCount() {
        return arr1.size();
    }

    public class ViewHolder  extends  RecyclerView.ViewHolder{
        TextView item_title,item_content,item_time;
        ImageView item_img;
        LinearLayout item_layout;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            item_title=itemView.findViewById(R.id.item_title);
            item_content=itemView.findViewById(R.id.item_content);
            item_img=itemView.findViewById(R.id.item_image);
            item_time=itemView.findViewById(R.id.item_time);
            item_layout=itemView.findViewById(R.id.item_layout);

        }
    }
}

3.2 MemoBean文件代码: 

一个javabean文件,为了存储备忘录的信息

package com.example.ts_menu.bean;

public class MemoBean {
        private  String title;
        private String content;
        private String imgpath;
        private String time;

        public MemoBean(String title, String content, String imgpath, String time) {
            this.title = title;
            this.content = content;
            this.imgpath = imgpath;
            this.time = time;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getContent() {
            return content;
        }

        public void setContent(String content) {
            this.content = content;
        }

        public String getImgpath() {
            return imgpath;
        }

        public void setImgpath(String imgpath) {
            this.imgpath = imgpath;
        }

        public String getTime() {
            return time;
        }

        public void setTime(String time) {
            this.time = time;
        }
    }


3.3 MydbHelper文件代码: 

数据库文件

package com.example.ts_menu.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDbHelper extends SQLiteOpenHelper {
    private  static String DBNAME="zsmemo.db";
    private  static int VERSION=1;
    //构造方法
    public MyDbHelper( Context context) {
        super(context, DBNAME, null, VERSION);
    }
    // 创建数据库
    @Override
    public void onCreate(SQLiteDatabase db) {
        //创建数据表
        db.execSQL("create table tb_memory(_id Integer primary key,title String (200),content String (2000),imgpath String (200),mtime String (50))");
    }
    //升级数据库
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    }
}

3.4 AddInfoActivity文件代码: 

 拍照功能直接闪退:

if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N)
{
    StrictMode.VmPolicy.Builder builder=new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());
}

使用上述代码防止闪退。

在拍照功能时应该将保存路径代码改为tmp_path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/image"+randtime+".jpg";

有效防止拍照后确定不了问题。

package com.example.ts_menu;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import android.os.Bundle;
import android.text.format.Time;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.UriUtils;
import com.bumptech.glide.Glide;
import com.example.ts_menu.db.MyDbHelper;
import com.example.ts_menu.db.MyDbHelper;

import java.io.File;
import java.io.IOException;

public class AddInfoActivity extends AppCompatActivity {
    //定义对象
    EditText edit_title,edit_content;
    Button btn_camera,btn_photo,btn_save;
    ImageView img_preview;
    String tmp_path,disp_path;
    MyDbHelper mhelper;
    SQLiteDatabase db;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_info);
        //1 绑定控件
        initView();

        //2 单击按钮、拍照、从图库中选择照片
        btnOnClick();

        //3 接受拍好照片、接受从图库当中选择的照片 ------方法:系统回调

        //4 把信息保存到数据库中
        btnSave();
    }


    //1 绑定控件-----代码
    private void initView() {
        edit_title=findViewById(R.id.editText_title);
        edit_content=findViewById(R.id.editText_content);
        btn_camera=findViewById(R.id.button_camera);
        btn_photo=findViewById(R.id.button_photo);
        img_preview=findViewById(R.id.imageView_preview);
        btn_save=findViewById(R.id.button_save);
        mhelper=new MyDbHelper(AddInfoActivity.this);
        db= mhelper.getWritableDatabase();
    }


    //2 单击按钮、拍照----------代码
    private void btnOnClick() {
        btn_camera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //拍照
                Time time=new Time();
                time.setToNow();
                String randtime=time.year+(time.month+1)+time.monthDay+time.hour+time.minute+time.second+"";
               // tmp_path= Environment.getExternalStorageDirectory()+"/image"+ randtime+".jpg";
                  tmp_path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).getAbsolutePath()+"/image"+randtime+".jpg";
                File imgfile=new File(tmp_path);
                try {
                    imgfile.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Intent intent=new Intent("android.media.action.IMAGE_CAPTURE");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imgfile) );
                startActivityForResult(intent,11);

            }
        });
        btn_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //选择照片
                Intent intent=new Intent("android.intent.action.GET_CONTENT");
                intent.setType("image/*");
                startActivityForResult(intent,22);
            }
        });
    }


    //3 接受拍好照片、接受从图库当中选择的照片 ------方法:系统回调

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        switch (requestCode){
            case 11:
                if(resultCode==RESULT_OK){
                    disp_path=tmp_path;
                    Glide.with(AddInfoActivity.this).load(disp_path).into(img_preview);
                }
                break;
            case 22:
                Uri imageuri=data.getData();
                if (imageuri==null)
                {
                    return;
                }
                disp_path=UriUtils.uri2File(imageuri).getPath();
                Glide.with(AddInfoActivity.this).load(disp_path).into(img_preview);
                break;
            default:
                break;
        }
    }

    //4 把信息保存到数据库中-------------代码
    private void btnSave() {
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //保存信息到数据库代码
                Time time=new Time();
                time.setToNow();
                ContentValues contentValues=new ContentValues();//一行
                contentValues.put("title",edit_title.getText().toString());//1行——1列
                contentValues.put("content",edit_content.getText().toString());//1行——3列
                contentValues.put("imgpath",disp_path);
                contentValues.put("mtime",time.year+"/"+(time.month+1)+"/"+time.monthDay);
                db.insert("tb_memory",null,contentValues);
                Toast.makeText(AddInfoActivity.this,"保存成功",Toast.LENGTH_SHORT).show();

                //跳转到主界面
                Intent intent=new Intent(AddInfoActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

}

3.5 LoginActivity文件代码:

package com.example.ts_menu;
import android.content.Intent;
import android.content.SharedPreferences;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import androidx.appcompat.app.AppCompatActivity;

public class LoginActivity extends AppCompatActivity {
    //定义对象
    private EditText edit_inputname,edit_inputpwd;
    private CheckBox check_reme;
    private Button btn_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        //1 绑定控件
        initView();
        //2 单击登录按钮,将用户名和密码保存起来
        btnloginonClick();
        //3 下次启动,直接显示用户名和密码
        displayinfo();
    }


    //1 绑定控件--------代码
    private void initView() {
        edit_inputname=findViewById(R.id.editText_inputname);
        edit_inputpwd=findViewById(R.id.editText_inputpwd);
        check_reme=findViewById(R.id.checkBox_reme);
        btn_login=findViewById(R.id.button_login);
    }

    //2 单击登录按钮,将用户名和密码保存起来----代码
    private void btnloginonClick() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //保存用户名和密码
                SharedPreferences.Editor editor=getSharedPreferences("myinfo",0).edit();
                editor.putString("name",edit_inputname.getText().toString());
                editor.putString("pwd",edit_inputpwd.getText().toString());
                editor.putBoolean("st",check_reme.isChecked());
                editor.commit();

                //跳转到第二页
                Intent intent=new Intent(LoginActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });
    }

    //3 下次启动,直接显示用户名和密码-----代码
    private void displayinfo() {
        String strname=getSharedPreferences("myinfo",0).getString("name","");
        String strpwd=getSharedPreferences("myinfo",0).getString("pwd","");
        Boolean status=getSharedPreferences("myinfo",0).getBoolean("st",false);
        if(status==true){
            edit_inputname.setText(strname);
            edit_inputpwd.setText(strpwd);
            check_reme.setChecked(true);
        }else{
            edit_inputname.setText("");
            edit_inputpwd.setText("");
            check_reme.setChecked(false);
        }
    }

}

3.6 MainActivity文件代码:

package com.example.ts_menu;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import android.os.Bundle;


import android.view.View;
import android.widget.Adapter;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.ts_menu.adapter.MemoAdapter;
import com.example.ts_menu.bean.MemoBean;
import com.example.ts_menu.db.MyDbHelper;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    //定义对象
    private Button btn_add;
    private RecyclerView recy_view;
    private MyDbHelper mhelper;
    SQLiteDatabase db;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //1 绑定控件
        initView();

        //2 对单击添加单击事件
        btnonclicknext();

        //3完善:从数据库获取数据,显示到RecyclerView控件里面
        recyDisplay();

    }


    //1 绑定控件-----------代码
    private void initView() {
        btn_add=findViewById(R.id.button_add);
        recy_view=findViewById(R.id.recy_view);
        mhelper=new MyDbHelper(MainActivity.this);
        db=mhelper.getWritableDatabase();
    }

    //2 对单击添加单击事件-----代码
    private void btnonclicknext() {
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //单击后跳转到一下页
                Intent intent=new Intent(MainActivity.this,AddInfoActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }

    //3完善:从数据库获取数据,显示到RecyclerView控件里面---------------代码
    private void recyDisplay() {
        //3.1准备数据-----------标题、内容、图片、时间(类)
        List<MemoBean> arr=new ArrayList();
        //从数据库里面取数据了
        Cursor  cursor=db.rawQuery("select * from tb_memory",null);
        while(cursor.moveToNext()){
            String mytitle=cursor.getString(cursor.getColumnIndex("title"));
            String mycontent=cursor.getString(cursor.getColumnIndex("content"));
            String myimgpath=cursor.getString(cursor.getColumnIndex("imgpath"));
            String mymtime=cursor.getString(cursor.getColumnIndex("mtime"));
            MemoBean memoBean=new MemoBean(mytitle,mycontent,myimgpath,mymtime);
            arr.add(memoBean);
        }
        cursor.close();


        //3.2 子布局 recy_item

        //3.3 数据------桥(适配器MemoAdapter)----------------子布局
        MemoAdapter adapter=new MemoAdapter(MainActivity.this,arr);
        //3.4 确定显示的方式
        StaggeredGridLayoutManager st=new StaggeredGridLayoutManager(2,StaggeredGridLayoutManager.VERTICAL);
        recy_view.setLayoutManager(st);

        //3.5 让数据显示出来
        recy_view.setAdapter(adapter);
    }

}

注意:activity文件中相关的名称报错,得换成自己所创建工程的名字。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1632970.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

NGINX发布动态页面的方法

一、建立 [rootserver100 html]# vim index.php [rootserver100 html]# pwd /usr/share/nginx/html 二、下载PHP文件 [rootserver100 conf.d]# dnf install php.x86_64 -y 正在更新 Subscription Management 软件仓库。 无法读取客户身份 本系统尚未在权利服务器中注册。可…

【C++】:const成员,取地址及const取地址操作符重载

目录 一&#xff0c;const成员二&#xff0c;取地址及const取地址操作符重载 一&#xff0c;const成员 将const修饰的“成员函数”称之为const成员函数&#xff0c;const修饰类成员函数&#xff0c;实际修饰该成员函数隐含的this指针&#xff0c;表明在该成员函数中不能对类的…

微信私域:如何管理多号聊天

我经常要在不同的微信号之间来回切换&#xff0c;这真是既麻烦又容易漏回消息&#xff0c;让我头疼不已。我试过了很多方法&#xff0c;但效果都不太理想。直到有一天&#xff0c;朋友分享了一个微信私域流量管理系统给我&#xff0c;我决定试试看。试用之后&#xff0c;我感觉…

机器学习理论基础—聚类算法

机器学习理论基础—聚类算法 聚类的距离计算 聚类&#xff1a;物以类聚。将相似的样本聚集到一起&#xff0c;使得同一类簇的样本尽可能接近,不同类簇的样本尽可能远离。&#xff08;无监督算法&#xff09; 对于距离的定义&#xff1a;满足下面的四个特点 非负性同一性对称性…

实验 | RT-Thread:L0

1 理解 官网文档&#xff1a;RT-Thread 简介 在 RT-Thread 系统中&#xff0c;任务通过线程实现的&#xff0c;RT-Thread 中的线程调度器也就是以上提到的任务调度器。RT-Thread与FreeRTOS是同等地位的东西&#xff0c;都是属于RTOS项目设置&#xff1a;RT-Thread 4.0.2 基于…

单链表进阶算法题精析

目录 一、回文链表 1.1 题目 1.2 题解 1.3 分析 二、带环链表I 2.1 题目 2.2 题解 2.3 分析 2.3.1为什么该思路可行&#xff1f; 2.3.2为什么只能快指针走两步&#xff1f; 三、带环链表II 3.1 题目 3.2 题解 3.3 分析 四、相交链表 4.1 题目 4.2 题解 4.3 分…

uniapp:K线图,支持H5,APP

使用KLineChart完成K线图制作,完成效果: 1、安装KLineChart npm install klinecharts2、页面中使用 <template><view class="index"><!-- 上方选项卡 --><view class="kline-tabs"><view :style="{color: current==ite…

git出错、文件无法删除、文件无法访问、文件或目录损坏且无法读取 等相关问题处理

一、错误历程与解决方案 1. 在用idea时&#xff0c;突然出现 部分git的命令无法使用&#xff0c;提示错误 2. 尝试删除项目文件夹&#xff0c;重新从git拉取代码 3.发现无法删除文件夹&#xff0c;删除操作没有任何反应&#xff0c;但是可以对文件夹重命名。 4.重新clone g…

【哈希】Leetcode 219. 存在重复元素 II

题目讲解 219. 存在重复元素 II 算法讲解 class Solution { public:bool containsNearbyDuplicate(vector<int>& nums, int k) {map<int, int>Hash;for(int i 0; i < nums.size(); i){//这一步说明前面已经出现过一次这样的数字了&#xff0c;所以用后面…

Android Studio的button点击事件

xml添加onClick调用方法 public class MainActivity extends AppCompatActivity {// 创建系统时间的文本控件TextView systemTimeTextView;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activit…

Docker基本操作 Linux里边操作

docker镜像操作命令: docker images:查看所有镜像; docker rmi:删除镜像 后边可以跟镜像的名字或者id指定要删除的镜像&#xff1b; docker pull:拉取镜像&#xff1b; docker push:推送镜像到服务&#xff1b; docker save :打包镜像 后边有用法; docker load:加载镜像&…

岚图汽车与东软睿驰签署战略合作协议

4月26日,东软睿驰与岚图汽车正式签署战略合作协议,双方将结合在各自领域拥有的产业资源、技术研发和资本运作等优势,聚焦智能化产品和应用,建立长期共赢的战略合作伙伴关系,通过不断探索未来新技术、新产业、新业态和新模式,围绕用户需求共同打造极致的智能出行体验。 图为岚图…

linux,从零安装mysql 8.0.30 ,并且更新至mysql 8.0.36

前言&#xff1a; 系统使用的CentOS 7&#xff0c;系统默认最小安装。 一、基础配置 配置虚拟机IP&#xff0c;需要更改的内容&#xff0c;如下红框中 修改之后 至此&#xff0c;基础配置完成。注意&#xff1a;此处虚拟机网络适配器使用的是&#xff1a;桥接模式 二、软件…

虚拟DOM

虚拟DOM&#xff08;Virtual DOM&#xff09;是现代前端框架&#xff08;如React、Vue等&#xff09;用于优化DOM操作性能的关键技术。Vue也采用了虚拟DOM来提高性能和提供更好的开发体验。以下是虚拟DOM在Vue中的详细解释&#xff1a; 什么是虚拟DOM&#xff1f; 虚拟DOM是一…

【分治算法】【Python实现】棋盘覆盖

文章目录 [toc]问题描述分治算法时间复杂性Python实现 个人主页&#xff1a;丷从心 系列专栏&#xff1a;分治算法 学习指南&#xff1a;Python学习指南 问题描述 在一个 2 k 2 k 2^{k} \times 2^{k} 2k2k个方格组成的棋盘中&#xff0c;若恰有一个方格与其他方格不同&…

vue3插槽的name和v-slot的研究

slot可以分为具名插槽和默认,默认插槽name是default 在父组件的template需要些v-slot/#,没写不生效,而在父组件下,而没被template包含的默认放在template且含有#default. 1)没写slot,可以不写template,也可写default的template2)写了name的slot,即使是default也必须些template…

STM32(垃圾桶开关盖)

封装超声波的代码 一、配置引脚的连接 二、配置 三、写代码 四、配置定时器 查找合适的定时器 其实这里的是remap&#xff08;复用&#xff09;&#xff0c;不重要 重要的是看Default才对 仔细查看之后发现还是能用的 先把开关灯封装好 再封装舵机 ----------------------…

谷粒商城实战(019 RabbitMQ-发送消息)

Java项目《谷粒商城》架构师级Java项目实战&#xff0c;对标阿里P6-P7&#xff0c;全网最强 总时长 104:45:00 共408P 此文章包含第248p-第p261的内容 介绍 MQ的三大特点&#xff1a;流量削峰、应用解耦、异步处理 redis是数据缓存&#xff0c;mq是任务缓存 AMQP的MODEL 第一…

静电纺丝左旋聚乳酸平行纳米纤维膜

静电纺丝左旋聚乳酸平行纳米纤维膜是一种特殊的纳米材料&#xff0c;它结合了静电纺丝技术的优点和左旋聚乳酸&#xff08;PLLA&#xff09;材料的特性。 静电纺丝技术是一种特殊的纤维制造工艺&#xff0c;在此过程中&#xff0c;高分子溶液或熔体在强电场中进行喷射纺丝。当电…

主成分分析(PCA):揭秘数据的隐藏结构

在数据分析的世界里&#xff0c;我们经常面临着处理高维数据的挑战。随着维度的增加&#xff0c;数据处理、可视化以及解释的难度也随之增加&#xff0c;这就是所谓的“维度的诅咒”。主成分分析&#xff08;PCA&#xff09;是一种强大的统计工具&#xff0c;用于减少数据的维度…