1. 程式人生 > 其它 >3月10日

3月10日

體溫上報二

RegisterActivity.java

package com.example.helloworld;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {

    private String realCode;
    private DBOpenHelper mDBOpenHelper;
    private Button mBtRegisteractivityRegister;
    private RelativeLayout mRlRegisteractivityTop;
    private ImageView mIvRegisteractivityBack;
    private LinearLayout mLlRegisteractivityBody;
    private EditText mEtRegisteractivityUsername;
    private EditText mEtRegisteractivityUserid;
    private EditText mEtRegisteractivityUserphone;
    private EditText mEtRegisteractivityUseraddress;
    private EditText mEtRegisteractivityPhonecodes;
    private ImageView mIvRegisteractivityShowcode;
    private RelativeLayout mRlRegisteractivityBottom;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        initView();

        mDBOpenHelper = new DBOpenHelper(this);

        //將驗證碼用圖片的形式顯示出來
        mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
        realCode = Code.getInstance().getCode().toLowerCase();
    }

    private void initView(){
        mBtRegisteractivityRegister = findViewById(R.id.bt_registeractivity_register);
        mRlRegisteractivityTop = findViewById(R.id.rl_registeractivity_top);
        mIvRegisteractivityBack = findViewById(R.id.iv_registeractivity_back);
        mLlRegisteractivityBody = findViewById(R.id.ll_registeractivity_body);
        mEtRegisteractivityUsername = findViewById(R.id.et_registeractivity_name);
        mEtRegisteractivityUserid = findViewById(R.id.et_registeractivity_id);
        mEtRegisteractivityUserphone = findViewById(R.id.et_registeractivity_phone);
        mEtRegisteractivityUseraddress = findViewById(R.id.et_registeractivity_address);
        mEtRegisteractivityPhonecodes = findViewById(R.id.et_registeractivity_phoneCodes);
        mIvRegisteractivityShowcode = findViewById(R.id.iv_registeractivity_showCode);
        mRlRegisteractivityBottom = findViewById(R.id.rl_registeractivity_bottom);

        /**
         * 註冊頁面能點選的就三個地方
         * top處返回箭頭、重新整理驗證碼圖片、註冊按鈕
         */
        mIvRegisteractivityBack.setOnClickListener(this);
        mIvRegisteractivityShowcode.setOnClickListener(this);
        mBtRegisteractivityRegister.setOnClickListener(this);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_registeractivity_back: //返回登入頁面
                Intent intent1 = new Intent(this, loginActivity.class);
                startActivity(intent1);
                finish();
                break;
            case R.id.iv_registeractivity_showCode:    //改變隨機驗證碼的生成
                mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                break;
            case R.id.bt_registeractivity_register:    //註冊按鈕
                //獲取使用者輸入的使用者名稱、密碼、驗證碼
                String username = mEtRegisteractivityUsername.getText().toString().trim();
                String userid = mEtRegisteractivityUserid.getText().toString().trim();
                String userphone = mEtRegisteractivityUserphone.getText().toString().trim();
                String useraddress = mEtRegisteractivityUseraddress.getText().toString().trim();
                String phoneCode = mEtRegisteractivityPhonecodes.getText().toString().toLowerCase();
                //註冊驗證
                if (!TextUtils.isEmpty(username) && !TextUtils.isEmpty(userid) && !TextUtils.isEmpty(phoneCode) && !TextUtils.isEmpty(userphone)
                        && !TextUtils.isEmpty(useraddress)    ) {
                    if (phoneCode.equals(realCode)) {
                        //將使用者名稱和密碼加入到資料庫中
                        mDBOpenHelper.add(username, userid,userphone,useraddress);
                        Intent intent2 = new Intent(this, MainActivity.class);
                        startActivity(intent2);
                        finish();
                        Toast.makeText(this,  "驗證通過,註冊成功", Toast.LENGTH_SHORT).show();
                    } else {
                        Toast.makeText(this, "驗證錯誤,註冊失敗", Toast.LENGTH_SHORT).show();
                    }
                }else {
                    Toast.makeText(this, "資訊不完整,註冊失敗", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }
}

MyDBHelper.java

package com.example.helloworld;

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

import androidx.annotation.Nullable;

public class MyDBHelper extends SQLiteOpenHelper {
    private Context context;
    public MyDBHelper(Context context,String name,int version) {
        super(context, name, null, version);
        this.context=context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql="create table IF NOT EXISTS personwendu(id integer primary key autoincrement, address varchar, name varchar, wendu varchar, dateandtime varchar,more varchar)";
        db.execSQL(sql);
        Toast.makeText(context,"資料加入成功",Toast.LENGTH_LONG).show();
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
}

DBOpenHelper.java

package com.example.helloworld;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import java.util.ArrayList;

public class DBOpenHelper extends SQLiteOpenHelper {
    private SQLiteDatabase db;
    public DBOpenHelper(Context context){
        super(context,"db_test",null,1);
        db = getReadableDatabase();
    }

   
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("CREATE TABLE IF NOT EXISTS user(" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                "username TEXT," +
                "userid TEXT,"+
                "userphone TEXT,"+
                "useraddress TEXT)"
        );
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS user");
        onCreate(db);
    }
  
    public void add(String username,String userid,String userphone,String useraddress){
        db.execSQL("INSERT INTO user (username,userid,userphone,useraddress) VALUES(?,?,?,?)",
                new Object[]{username,userid,userphone,useraddress});
    }
    public void delete(String userphone){
        db.execSQL("DELETE FROM user WHERE userphone = "+userphone);
    }
    public void updata(String userphone){
        db.execSQL("UPDATE user SET userphone = ?",new Object[]{userphone});
    }

   
    public ArrayList<User> getAllData(){

        ArrayList<User> list = new ArrayList<User>();
        Cursor cursor = db.query("user",null,null,null,null,null,"username DESC");
        cursor.moveToFirst();
        while(cursor.moveToNext()){
            String username = cursor.getString(cursor.getColumnIndex("username"));
            String userid = cursor.getString(cursor.getColumnIndex("userid"));
            String userphone = cursor.getString(cursor.getColumnIndex("userphone"));
            String useraddress = cursor.getString(cursor.getColumnIndex("useraddress"));
            list.add(new User(username,userid,userphone,useraddress));
        }
        Log.v("資料庫查詢結果:",list.toString());
        return list;
    }
}