android studio简单的密码日记本,实现增删改查功能

简单的记事本

软件名字:一本日记
软件功能 :简单的密码日记本,实现增删改查功能
软件的使用知识点:

​ 布局方面:线性布局,相对布局

​ 控件方面:常见控件,List View, Navigation View(滑动菜单),FloatingActionButton (悬浮按钮)

​ 后台方面:主要是Adapter和Sq Lite,Intent

项目的结构如下图:

[外链图片转存失败(img-OocaH9L5-1563628277567)(C:\Users\weng\Desktop\安卓期末1.png)]android studio简单的密码日记本,实现增删改查功能_第1张图片

分析软件的组成:
展示核心的代码
1,登录
        
     	 
    	

2注册

	
    
    
	

注册的业务逻辑:

public class RegistActivity extends AppCompatActivity {
    private EditText edt_setname;
    private EditText edt_setpassword;
    private  EditText edt_confirm_password;
    private Button btn_register;
    String name,password,id;
    private MyDB myDB;
    ContentValues contentValues=new ContentValues();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();//隐藏标题栏*/
        setContentView(R.layout.activity_regist);
        initUI();
        myDB=new MyDB(this);
        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SQLiteDatabase db=myDB.getWritableDatabase();
                name = edt_setname.getText().toString();
                password = edt_setpassword.getText().toString();
                if (edt_setname.getText().toString().contains(" ") || edt_setpassword.getText().toString().contains(" ")) {
                    Toast.makeText(RegistActivity.this, "输入的用户名或密码不能包含空格", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if(edt_setpassword.getText().toString().length()==0||edt_setname.getText().toString().length()==0){
                    Toast.makeText(RegistActivity.this, "请输入用户名或密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                else if(!edt_confirm_password.getText().toString().equals(edt_setpassword.getText().toString())){
                    Toast.makeText(RegistActivity.this, "两次密码不一致,请确认再注册", Toast.LENGTH_SHORT).show();
                    return;
                }
                if(login(name)){
                    Toast.makeText(RegistActivity.this, "该用户名已注册", Toast.LENGTH_SHORT).show();
                    return;
                }
                contentValues.put("username", name);
                contentValues.put("password", password);
                db.insert("tb_user", null, contentValues);
                String sql="select user_id from tb_user where username=? and password=?";
                Cursor cursor = db.rawQuery(sql, new String[] {name, password});
                if(cursor.moveToNext()){//id
                    id=Integer.toString(cursor.getInt(0));//防止数据库的值null和0 分不清
                }
                cursor.close();
                db.close();
                Toast.makeText(RegistActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                new Thread(){
                    @Override
                    public void run() {
                        super.run();
                        try {
                            Thread.sleep(1000);
                            Intent intent = new Intent(RegistActivity.this,LoginActivity.class);
                            intent.putExtra("name",edt_setname.getText().toString());
                            intent.putExtra("password",edt_setpassword.getText().toString());
                            setResult(1, intent);//返回页面1
                            finish();
                          //  startActivity(intent);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }

    /**
     * 初始化View :initUI()
     */
    public void initUI(){
        edt_setname=(EditText)findViewById(R.id.edt_setusername);
        edt_setpassword=(EditText)findViewById(R.id.edt_setpassword);
        edt_confirm_password=(EditText)findViewById(R.id.edt_confirm_password);
        btn_register=(Button)findViewById(R.id.btn_register);
    }
    /**
     * 查询数据库是否有重复的
     * 进行数据的查询   login()
     */
    public boolean login(String name){
        SQLiteDatabase db=myDB.getWritableDatabase();
        String sql="select * from tb_user where username=?";
        Cursor cursor=db.rawQuery(sql,new String[]{name});
        if (cursor.getCount()!=0){
            cursor.close();
            return true;
        }
        return false;
    }
}

3主页面

<ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
    ListView>
    <android.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="30dp"
        android:layout_marginRight="20dp"
        app:fabSize="mini"
        app:srcCompat="@android:drawable/ic_input_add"
        android:id="@+id/floactAdd"
        app:backgroundTint="@color/colorPrimaryDark" />
RelativeLayout>
<android.support.design.widget.NavigationView
    android:layout_gravity="left"
    app:headerLayout="@layout/head"
    app:menu="@menu/meun_main"
    android:id="@+id/navigat"
    android:layout_width="230dp"
    android:layout_height="match_parent"/>

主页面的业务逻辑:

public class IndexActivity extends AppCompatActivity {
    List<Note> mList;
    private FloatingActionButton fdb;
    private NavigationView navigationView;
    private ListView listView;
    private MyAdapter myAdapter;
    private MyDB myDB;
    private Cursor cursor;
    private SQLiteDatabase dbreader;;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();//隐藏标题栏
        setContentView(R.layout.activity_test);
        listView = (ListView) findViewById(R.id.list);
        navigationView = (NavigationView) findViewById(R.id.navigat);
        View view1 = navigationView.getHeaderView(0);//通过nav来查询head文件
        TextView wc = view1.findViewById(R.id.welcome);
        myDB = new MyDB(this);
        dbreader = myDB.getReadableDatabase();
        wc.setText("欢迎你:" + LoginActivity.username);
        Toast.makeText(IndexActivity.this, "登录成功,欢迎回来!", Toast.LENGTH_SHORT).show();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                Intent intent = new Intent(IndexActivity.this, showActivity.class);
                intent.putExtra("ID", mList.get(position).getId());
                intent.putExtra("content", mList.get(position).getContent());
                intent.putExtra("time", mList.get(position).getTime());
                startActivity(intent);
        }
        });
        fdb = (FloatingActionButton) findViewById(R.id.floactAdd);
        fdb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(IndexActivity.this, AddContentActivity.class);
                startActivity(intent);
            }
        });
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()){
                    case R.id.prsetposs:
                        Intent intent1=new Intent(IndexActivity.this,setpasswordActivity.class);
                        intent1.putExtra("username",LoginActivity.username);
                        startActivity(intent1);
                        break;
                    case R.id.showtime:
                        Toast.makeText(IndexActivity.this,AddContentActivity.getTime(),Toast.LENGTH_LONG).show();
                        break;
                    case R.id.prsize:
                        Toast.makeText(IndexActivity.this,IndexActivity.this.allDBNum() + "个用户使用了软件",Toast.LENGTH_LONG).show();
                        break;
                    case  R.id.success:
                        Intent intent=new Intent(IndexActivity.this,LoginActivity.class);
                        Toast.makeText(IndexActivity.this,"退出成功",Toast.LENGTH_LONG).show();
                        startActivity(intent);
                }
                return true;
            }
        });
    }
    public void selectDb() {
        mList = new ArrayList<>();
        cursor = dbreader.rawQuery("SELECT * FROM tb_note WHERE userID = ?", new String[]{LoginActivity.userID + ""});
        while (cursor.moveToNext()) {
            Note note = new Note();
            int id = cursor.getInt(cursor.getColumnIndex("ID"));
            String time = cursor.getString(cursor.getColumnIndex("time"));
            String content = cursor.getString(cursor.getColumnIndex("content"));
            note.setTime(time);
            note.setContent(content);
            note.setId(id);
            mList.add(note);
        }
        cursor.close();
        myAdapter = new MyAdapter(this, mList);
        listView.setAdapter(myAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        selectDb();
    }
    /**
     *查询用户的个数 allDBNum()
     */
    public int allDBNum( ){
        String sql = "select count(*) count  from tb_user";
        Cursor cursor = dbreader.rawQuery(sql, null);
         cursor.moveToFirst();
        int count= cursor.getInt(0);
        if (cursor.getCount()!=0){
            cursor.close();
        }
        return count;
    }
}

4 List View 布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_gravity="center_vertical"
    android:orientation="vertical">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_content"
        android:textColor="#000"
        android:textSize="20sp"
        android:text="tv"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/list_time"
        android:textColor="#000"
        android:textSize="20sp"
        android:text="time"/>
LinearLayout>

5List View适配器:

public class MyAdapter extends BaseAdapter {
    private Context mContext;
    private List<Note> mList;
    private LinearLayout mlayout;

    public MyAdapter(Context mContext, List<Note> mList){
        this.mContext=mContext;
        this.mList=mList;
    }
    @Override
    public int getCount() {
        return mList.size();
    }
    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        mlayout=(LinearLayout) inflater.inflate(R.layout.listre,null);
        TextView content=(TextView)mlayout.findViewById(R.id.list_content);
        TextView time=(TextView)mlayout.findViewById(R.id.list_time);
        content.setText(mList.get(position).getContent());
        time.setText(mList.get(position).getTime());
        return mlayout;
    }
}

6数据库文件:

ublic class MyDB extends SQLiteOpenHelper{
    //用户表
    public static  final  String CREATE_USER="create table tb_user(" +
            "user_id integer primary key autoincrement," +
            "username varchar(50)," +
            "password varchar(50))";
    //
    public static final String CREATE_NOTE="create table tb_note("+
            "ID integer primary key AUTOINCREMENT,"+
            "content TEXT NOT NULL,"+
            "userID  integer,"+
            "time TEXT NOT NULL)";
    //上下文
    private  Context mcontext;
    public static final  String CREATE_NOTES="";
    public MyDB(Context context) {
        super(context, "MyDB.db", null, 2);
        mcontext=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_USER);
        db.execSQL(CREATE_NOTE);
        Toast.makeText(mcontext,"创建成功!",Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        
    }
}

7Note的实体类:

public class Note {
    private Integer id;
    private Integer userID;
    private String content;
    private String time;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getUserID() {
        return userID;
    }

    public void setUserID(Integer userID) {
        this.userID = userID;
    }

    public String getContent() {
        return content;
    }

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

    public String getTime() {
        return time;
    }

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

8添加内容的页面:

<EditText
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:gravity="top"
    android:hint="开始记录吧"/>
    <Button
        android:id="@+id/save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="保存"
        android:onClick="save"/>
    <Button
        android:id="@+id/cancle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="取消"
        android:onClick="cancle"/>

添加内容的业务逻辑:

private EditText met;
private MyDB myDB;
private SQLiteDatabase mysqldb;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
    getSupportActionBar().hide();//隐藏标题栏
    setContentView(R.layout.activity_add_content);
    met=(EditText)findViewById(R.id.text);
    myDB=new MyDB(this);
    mysqldb=myDB.getWritableDatabase();
}
public void save(View v) {
    DdAdd();
    finish();
}
public void cancle(View v) {
    met.setText("");
    finish();
}
public void DdAdd(){
    ContentValues cv = new ContentValues();
    cv.put("content",met.getText().toString());
    cv.put("time",getTime());
    cv.put("userID", LoginActivity.userID);
    mysqldb.insert("tb_note",null,cv);//插入数据
}
public static String getTime(){
    SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
    Date date=new Date();
    String str=sdf.format(date);
    return str;
}

9显示内容的页面:

<TextView
    android:id="@+id/showtime"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_marginLeft="10dp"
    android:textSize="20sp"/>
<EditText
    android:id="@+id/showtext"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="top"
    android:layout_weight="1"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:textColor="#000"
    android:hint="记事本"/>
    <Button
        android:id="@+id/delete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="删除"
        android:onClick="delete"/>
    <Button
        android:id="@+id/update"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="修改"
        android:onClick="update" />
    <Button
        android:id="@+id/back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="返回"
        android:onClick="goBack"/>
LinearLayout>

显示页面的业务逻辑:

public class showActivity extends AppCompatActivity {
    private EditText mtext;
    private TextView time;
    private MyDB myDB;
    private SQLiteDatabase msql;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();//隐藏标题栏
        setContentView(R.layout.activity_show);
        mtext=(EditText) findViewById(R.id.showtext);
        time=(TextView)findViewById(R.id.showtime);
        myDB=new MyDB(this);
        msql=myDB.getWritableDatabase();
        mtext.setInputType(InputType.TYPE_CLASS_TEXT);//开启可以编写
        mtext.setText(getIntent().getStringExtra("content"));
        time.setText(getIntent().getStringExtra("time"));
    }
    /**
     * 删除 delete
     * @param v
     */
    public void delete(View v) {
          int id = getIntent().getIntExtra("ID",0);
          msql.delete("tb_note","ID = " + id,null);
          finish();
          /**
          * 修改 update
          */
    }public void update(View v){
        int id = getIntent().getIntExtra("ID",0);
        String content=mtext.getText().toString();
        msql.execSQL("UPDATE tb_note SET content=? WHERE ID = ?",
                new String[]{content,id+""});
        finish();
    }
    /**
     * 关闭
     * @param v
     */
    public void goBack(View v) {
        finish();
    }

}

10修改密码的页面:

<TextView
    android:layout_marginTop="-120dp"
    android:text="修改密码"
    android:textSize="34dp"
    android:layout_marginBottom="20dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
<EditText
    android:id="@+id/setpass"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="输入新密码"
    />
    <Button
        android:id="@+id/yes"
        android:text="确定"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:onClick="yes"/>
    <Button
        android:id="@+id/no"
        android:text="取消"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:onClick="no"/>
LinearLayout>

修改密码的业务逻辑:

public class setpasswordActivity extends AppCompatActivity {
    private EditText setpass;
    private MyDB myDB;
    private SQLiteDatabase msql;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);//隐藏状态栏
        getSupportActionBar().hide();
        setContentView(R.layout.activity_setpassword);
        myDB=new MyDB(this);
        msql=myDB.getWritableDatabase();
        setpass=(EditText)findViewById(R.id.setpass);

    }
    public  void yes(View v){
        String username = getIntent().getStringExtra("username");
        String pass=setpass.getText().toString();
        msql.execSQL("UPDATE  tb_user SET password=? WHERE  username=?",
                new String[]{pass,username});
        Toast.makeText(setpasswordActivity.this, "密码修改完了,请记住现在的密码", Toast.LENGTH_SHORT).show();
        Intent intent2=new Intent(setpasswordActivity.this,LoginActivity.class);
        startActivity(intent2);
        finish();
    }
    public void no(View v){
        Intent intent=new Intent(setpasswordActivity.this,IndexActivity.class);
        startActivity(intent);
        finish();
    }
}

软件的展示图片:

登录:
android studio简单的密码日记本,实现增删改查功能_第2张图片
注册:

android studio简单的密码日记本,实现增删改查功能_第3张图片
主页面
android studio简单的密码日记本,实现增删改查功能_第4张图片
主页面2
android studio简单的密码日记本,实现增删改查功能_第5张图片
添加
android studio简单的密码日记本,实现增删改查功能_第6张图片
修改
android studio简单的密码日记本,实现增删改查功能_第7张图片

你可能感兴趣的:(android studio简单的密码日记本,实现增删改查功能)