安卓程序-日记

部分代码还有待优化,奋斗吧,呆萌一族 ~ ~ ~

功能实现

  1. 实现注册与登录功能
  2. 可以在页面中编辑日记
  3. 使用数据库对日记内容进行存储


源码分析

  1. 在数据库中定义title作为标题,定义content作为内容
    public static final String CREATE_BOOK = "create table Book ("
                + "id integer primary key autoincrement,"
                + "title text,"
                + "content text)";
  2. 使用SharePreferferences这样的键值对来进行注册与登录
    SharedPreferences pref = getSharedPreferences("myShar", MODE_APPEND);
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString(user, p1);
                            editor.apply();

  3. 进行登录的用户名密码验证
    if (!user.equals("")) {
                        SharedPreferences preferences = getSharedPreferences("myShar", MODE_APPEND);
                        String pass = password.getText().toString();
                        if (pass.equals(preferences.getString(user, ""))) {
                            Intent intent = new Intent();
                            intent.putExtra("name",user);
                            intent.setClass(Login.this, Main.class);
                            Login.this.startActivity(intent);
                        } else {
                            Toast.makeText(Login.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
    
                    } else {
                        Toast.makeText(Login.this, "用户名不为空", Toast.LENGTH_SHORT).show();
                    }

  4. 从数据库中取出数据
    dbHelper = new MyDatabaseHelper(this, name + ".db", null, 2);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            Cursor cursor = db.query("Book", null, null, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    String title = cursor.getString(cursor.getColumnIndex("title"));
                    String content = cursor.getString(cursor.getColumnIndex("content"));
                    Fruit fruit = new Fruit(title, content);
                    fruitList.add(fruit);
                    Log.d("Main.java", "title:" + title);
                    Log.d("Main.java", "content" + content);
                } while (cursor.moveToNext());
            }
            cursor.close();

  5. 确保实时更新日记,对添加的数据能够得到及时反馈
    @Override
        protected void onResume() {
            super.onResume();
            fruitList = new ArrayList<Fruit>();
            init();
            FruitAdapter fruitAdapter = new FruitAdapter(Main.this, R.layout.mylist, fruitList);
            listView.setAdapter(fruitAdapter);
        }

成品图片


全部代码

  • MainActivity:第一个页面
    package com.example.application.application;
    
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        private Button login;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            login = (Button) findViewById(R.id.login);
    
            login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(MainActivity.this, Login.class);
                    MainActivity.this.startActivity(intent);
                }
            });
    
        }
    
    
    }
    

  • Login登录页面:
    package com.example.application.application;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class Login extends Activity {
    
        private Button register;
        private Button go_login;
        private EditText username;
        private EditText password;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);
    
            register = (Button) findViewById(R.id.register);
            go_login = (Button) findViewById(R.id.go_login);
            username = (EditText) findViewById(R.id.login_username);
            password = (EditText) findViewById(R.id.login_password);
    
            register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.setClass(Login.this, Register.class);
                    Login.this.startActivity(intent);
                }
            });
    
            go_login.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String user = username.getText().toString();
                    if (!user.equals("")) {
                        SharedPreferences preferences = getSharedPreferences("myShar", MODE_APPEND);
                        String pass = password.getText().toString();
                        if (pass.equals(preferences.getString(user, ""))) {
                            Intent intent = new Intent();
                            intent.putExtra("name", user);
                            intent.setClass(Login.this, Main.class);
                            Login.this.startActivity(intent);
                        } else {
                            Toast.makeText(Login.this, "用户名或密码错误", Toast.LENGTH_SHORT).show();
                        }
    
                    } else {
                        Toast.makeText(Login.this, "用户名不为空", Toast.LENGTH_SHORT).show();
                    }
    
    
                }
            });
    
        }
    }
    

  • 注册页面:
    package com.example.application.application;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class Register extends Activity {
    
        private EditText username;
        private EditText pass1;
        private EditText pass2;
        private Button reset;
        private Button confirm_register;
        private MyDatabaseHelper dbHelper;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.register);
    
            username = (EditText) findViewById(R.id.username);
            pass1 = (EditText) findViewById(R.id.pass1);
            pass2 = (EditText) findViewById(R.id.pass2);
            reset = (Button) findViewById(R.id.reset);
            confirm_register = (Button) findViewById(R.id.confirm_register);
    
            reset.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    username.setText("");
                    pass1.setText("");
                    pass2.setText("");
                    Toast.makeText(Register.this, "已重置", Toast.LENGTH_SHORT).show();
                }
            });
    
            confirm_register.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String user = username.getText().toString();
                    if (!user.equals("")) {
                        String p1 = pass1.getText().toString();
                        String p2 = pass2.getText().toString();
                        if (p1.equals(p2)) {
                            SharedPreferences pref = getSharedPreferences("myShar", MODE_APPEND);
                            SharedPreferences.Editor editor = pref.edit();
                            editor.putString(user, p1);
                            editor.apply();
                            //创建专属库
                            dbHelper = new MyDatabaseHelper(Register.this,user+".db",null,1);
                            dbHelper.getWritableDatabase();
                            //跳转
                            Intent intent = new Intent();
                            intent.setClass(Register.this, Login.class);
                            Register.this.startActivity(intent);
                        } else {
                            Toast.makeText(Register.this, "两次密码不正确", Toast.LENGTH_SHORT).show();
                        }
                    } else {
                        Toast.makeText(Register.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
                    }
                }
            });
        }
    }
    

  • 作为数据库的辅助类:
    package com.example.application.application;
    
    import android.content.Context;
    import android.database.sqlite.SQLiteDatabase;
    import android.database.sqlite.SQLiteOpenHelper;
    import android.widget.Toast;
    
    
    public class MyDatabaseHelper extends SQLiteOpenHelper {
    
        public static final String CREATE_BOOK = "create table Book ("
                + "id integer primary key autoincrement,"
                + "title text,"
                + "content text)";
        private Context mContext;
    
        public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, name, factory, version);
            mContext = context;
        }
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_BOOK);
            Toast.makeText(mContext, "创建成功", Toast.LENGTH_SHORT).show();
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        }
    }
    
  • 主界面:
    package com.example.application.application;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.database.Cursor;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    public class Main extends Activity {
        private Button add;
        private TextView username;
        private ListView listView;
        private List<Fruit> fruitList = new ArrayList<Fruit>();
        String name;
        private MyDatabaseHelper dbHelper;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            username = (TextView) findViewById(R.id.name);
            listView = (ListView) findViewById(R.id.list_view);
    
            final Intent intent = getIntent();
            name = intent.getStringExtra("name");
            username.setText(name);
            //init();
            add = (Button) findViewById(R.id.add_log);
    
    
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent1 = new Intent();
                    intent1.putExtra("name", name);
                    intent1.setClass(Main.this, SetList.class);
                    Main.this.startActivity(intent1);
                }
            });
    
            //FruitAdapter fruitAdapter = new FruitAdapter(Main.this, R.layout.mylist, fruitList);
            //listView.setAdapter(fruitAdapter);
    
        }
    
        private void init() {
            //从数据库中取数据
            dbHelper = new MyDatabaseHelper(this, name + ".db", null, 2);
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            Cursor cursor = db.query("Book", null, null, null, null, null, null);
            if (cursor.moveToFirst()) {
                do {
                    String title = cursor.getString(cursor.getColumnIndex("title"));
                    String content = cursor.getString(cursor.getColumnIndex("content"));
                    Fruit fruit = new Fruit(title, content);
                    fruitList.add(fruit);
                    Log.d("Main.java", "title:" + title);
                    Log.d("Main.java", "content" + content);
                } while (cursor.moveToNext());
            }
            cursor.close();
    
    
        }
    
        @Override
        protected void onResume() {
            super.onResume();
            fruitList = new ArrayList<Fruit>();
            init();
            FruitAdapter fruitAdapter = new FruitAdapter(Main.this, R.layout.mylist, fruitList);
            listView.setAdapter(fruitAdapter);
        }
    }
    
  • 两个作为List的列表辅助类:
    package com.example.application.application;
    
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ArrayAdapter;
    import android.widget.TextView;
    
    import java.util.List;
    
    public class FruitAdapter extends ArrayAdapter<Fruit> {
        private int resourceId;
    
        public FruitAdapter(Context context, int textViewResourceId, List<Fruit> objects) {
            super(context, textViewResourceId, objects);
            resourceId = textViewResourceId;
        }
    
        @Override
        public View getView(int position, View contentView, ViewGroup parent) {
            Fruit fruit = getItem(position);
            View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            TextView my_title = (TextView) view.findViewById(R.id.my_title);
            TextView my_content = (TextView) view.findViewById(R.id.my_content);
            my_title.setText(fruit.getTitle());
            my_content.setText(fruit.getContent());
            return view;
        }
    }
    

    package com.example.application.application;
    
    public class Fruit {
        private String title;
        private String content;
    
        public Fruit(String title,String content){
            this.title = title;
            this.content = content;
        }
    
        public String getTitle() {
            return title;
        }
    
        public String getContent() {
            return content;
        }
    }
    

  • 最后一个页面:新建日记的页面:
    package com.example.application.application;
    
    import android.app.Activity;
    import android.content.ContentValues;
    import android.content.Intent;
    import android.database.sqlite.SQLiteDatabase;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    
    public class SetList extends Activity {
    
        private Button set_confirm;
        private EditText set_title;
        private EditText set_content;
        private MyDatabaseHelper dbHelper;
    
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.setlist);
            set_title = (EditText) findViewById(R.id.set_title);
            set_content = (EditText) findViewById(R.id.set_content);
            set_confirm = (Button) findViewById(R.id.set_confirm);
    
    
            final Intent intent = getIntent();
            final String name = intent.getStringExtra("name");
    
            set_confirm.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String title = set_title.getText().toString();
                    String content = set_content.getText().toString();
                    if (!(title.equals("") || content.equals(""))) {
                        dbHelper = new MyDatabaseHelper(SetList.this, name + ".db", null, 2);
                        SQLiteDatabase database = dbHelper.getWritableDatabase();
                        ContentValues values = new ContentValues();
                        values.put("title", title);
                        values.put("content", content);
                        database.insert("Book", null, values);
                        Toast.makeText(SetList.this, "日志添加成功", Toast.LENGTH_SHORT).show();
                        finish();
                    } else {
                        Toast.makeText(SetList.this, "请填写完整", Toast.LENGTH_SHORT).show();
                    }
                }
            });
    
    
        }
    }
    

  • 之后就是布局文件了

  • 主界面的布局:
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/main2"
        tools:context="com.example.application.application.MainActivity">
    
        <Button
            android:id="@+id/login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:background="@android:color/holo_blue_light"
            android:text="进入应用"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="0dp" />
    
    
    </android.support.constraint.ConstraintLayout>
    



  • 登录页面:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10sp"
            android:text="Welcome"
            android:textSize="50sp" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView3"
            android:layout_marginTop="102dp"
            android:text="用户名"
            android:textSize="20sp" />
    
        <EditText
            android:id="@+id/login_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView2"
            android:ems="10" />
    
        <TextView
            android:id="@+id/textView4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/login_username"
            android:paddingTop="20sp"
            android:text="密码"
            android:textSize="20sp" />
    
        <EditText
            android:id="@+id/login_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView4"
            android:inputType="textPassword"
            android:paddingBottom="20sp" />
    
        <Button
            android:id="@+id/register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/login_password"
            android:text="注册" />
    
        <Button
            android:id="@+id/go_login"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/login_password"
            android:text="登录" />
    
    </RelativeLayout>

  • 注册界面:
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/textView5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:gravity="center"
            android:padding="20sp"
            android:text="欢迎注册"
            android:textSize="30sp" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView5"
            android:layout_marginTop="142dp"
            android:text="用户名"
            android:textSize="20sp"/>
    
        <EditText
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="textPersonName"
            android:layout_below="@+id/textView"
            android:layout_alignParentStart="true"
            android:paddingBottom="10sp"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="密码"
            android:id="@+id/textView6"
            android:layout_below="@+id/username"
            android:layout_alignParentStart="true"
            android:textSize="20sp"/>
    
        <EditText
            android:id="@+id/pass1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView6"
            android:inputType="textPassword"
            android:paddingBottom="10sp" />
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/pass1"
            android:layout_alignParentStart="true"
            android:textSize="20sp"
            android:text="确认密码"
            android:id="@+id/textView7" />
    
        <EditText
            android:id="@+id/pass2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/textView7"
            android:inputType="textPassword" />
    
        <Button
            android:id="@+id/confirm_register"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_below="@+id/pass2"
            android:text="确认" />
    
        <Button
            android:id="@+id/reset"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/pass2"
            android:text="重置" />
    </RelativeLayout>
  • 日记列表的主界面:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Welcome:"
                android:textSize="30sp"
                android:id="@+id/textView8" />
    
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="30sp"
                android:layout_alignBaseline="@+id/add_log"
                android:layout_alignBottom="@+id/add_log"
                android:layout_toEndOf="@+id/textView8" />
    
            <Button
                android:id="@+id/add_log"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="添加日记"
                android:layout_alignParentTop="true"
                android:layout_alignParentEnd="true" />
    
        </RelativeLayout>
    
        <ListView
            android:id="@+id/list_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </ListView>
    
    
    </LinearLayout>

  • 用来定制列表项的布局文件:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:id="@+id/my_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"/>
    
        <TextView
            android:id="@+id/my_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:maxLines="10"
            android:minLines="5"
            android:scrollbars="vertical"
            android:padding="10sp"/>
    
    </LinearLayout>

  • 用来创建日记的布局文件:
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="20sp"
            android:text="创建日记"
            android:textSize="20sp" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="标题:"
            android:textSize="20sp" />
    
        <EditText
            android:id="@+id/set_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
        <TextView
            android:id="@+id/textView9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="日记内容:"
            android:textSize="20sp" />
    
        <EditText
            android:id="@+id/set_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:maxLines="10"
            android:minLines="5"
            android:scrollbars="vertical"/>
    
        <Button
            android:id="@+id/set_confirm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="确认" />
    
    
    </LinearLayout>







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呆萌的代Ma

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值