今天学习了Android Studio
对于昨日的错误找到了对应的解决方案
没有在前面设置时间监听器,所以无法触发R.id事件
今日学习了链接本地数据库,
成果
对于图一,代码如下
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:background="#f3f3f3" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="180dp" android:text="@string/app_name" android:textColor="@color/black" android:textSize="20dp" android:textStyle="bold" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:padding="20dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/ic_main_background" android:padding="5dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名 : " android:textColor="@color/black" android:textSize="18dp" /> <EditText android:id="@+id/main_ed_zhang" android:layout_width="match_parent" android:layout_height="wrap_content" android:imeOptions="actionSearch" android:maxLength="10" android:hint="请输入用户名/ID" android:maxLines="1" android:singleLine="true" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:background="@drawable/ic_main_background" android:padding="5dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密 码 : " android:textColor="@color/black" android:textSize="18dp" /> <EditText android:id="@+id/main_ed_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:imeOptions="actionSearch" android:inputType="textPassword" android:maxLength="10" android:hint="请输入密码" android:maxLines="1" android:singleLine="true" android:textColorHint="#bcbcbc" /> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:padding="5dp"> <Button android:id="@+id/main_bu_deng" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="登录" android:onClick="onClick" android:textStyle="bold" /> <Button android:id="@+id/main_bu_zhu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_weight="1" android:text="注册" android:onClick="onClick" android:textStyle="bold" /> </LinearLayout> </LinearLayout>
Java文件
package com.example.meiri; import androidx.appcompat.app.AppCompatActivity; 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.Toast; import com.example.meiri.db.DBOpenHelper; import com.example.meiri.db.User; import java.util.ArrayList; public class MainActivity extends AppCompatActivity { private DBOpenHelper mDBOpenHelper; private EditText mEtRegisteractivityzhang; private EditText mEtRegisteractivitypassword; private Button mBtRegisteractivitydeng; private Button mBtRegisteractivityzhu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mDBOpenHelper = new DBOpenHelper(this); } private void initView() { // 初始化控件 mEtRegisteractivityzhang = findViewById(R.id.main_ed_zhang); mEtRegisteractivitypassword = findViewById(R.id.main_ed_password); mBtRegisteractivitydeng = findViewById(R.id.main_bu_deng); mBtRegisteractivityzhu = findViewById(R.id.main_bu_zhu); // 设置点击事件监听器 mBtRegisteractivitydeng.setOnClickListener(this::onClick); mBtRegisteractivityzhu.setOnClickListener(this::onClick); } public void onClick(View view) { switch (view.getId()) { // 跳转到注册界面 case R.id.main_bu_zhu: startActivity(new Intent(this, RegisterActivity.class)); finish(); break; case R.id.main_bu_deng: String id = mEtRegisteractivityzhang.getText().toString().trim(); String password = mEtRegisteractivitypassword.getText().toString().trim(); if (!TextUtils.isEmpty(id) && !TextUtils.isEmpty(password)) { ArrayList<User> data = mDBOpenHelper.getAllData(); boolean match = false; for (int i = 0; i < data.size(); i++) { User user = data.get(i); if (id.equals(user.getName()) && password.equals(user.getPassword())/*&&phoneCode.equals(realCode)*/) { match = true; break; } else { match = false; } } if (match) { Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(this,DakaActivity.class); startActivity(intent); finish();//销毁此Activity } else { Toast.makeText(this, "用户名或密码不正确,请重新输入", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(this, "请输入你的用户名或密码", Toast.LENGTH_SHORT).show(); } break; } } }
对于图二,代码如下;
xml文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#f3f3f3" android:orientation="vertical" tools:context=".RegisterActivity"> <ImageView android:id="@+id/iv_registeractivity_back" android:layout_width="50dp" android:layout_height="50dp" android:layout_marginLeft="10dp" android:src="@mipmap/back" android:layout_marginTop="15dp" android:clickable="true" android:onClick="onClick" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" android:gravity="center" android:textStyle="bold" android:textSize="30dp" android:textColor="@color/black" android:layout_marginTop="20dp"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="18dp" android:layout_marginRight="18dp" android:background="@drawable/ic_main_background" android:gravity="center" android:orientation="vertical" android:paddingLeft="43dp" android:paddingRight="31dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="学号" android:textSize="16dp" /> <EditText android:id="@+id/et_registeractivity_id" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="20dp" android:background="@null" android:maxLength="8" android:maxLines="1" android:imeOptions="actionSearch" android:singleLine="true" android:hint="请输入学号" android:textColor="#000000" android:textColorHint="#bcbcbc" android:textSize="18sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="23dp" android:layout_marginBottom="23dp" android:background="#f3f3f3" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" android:textSize="16dp" /> <EditText android:id="@+id/et_registeractivity_username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:background="@null" android:hint="请输入用户名/姓名" android:textColor="@color/black" android:imeOptions="actionSearch" android:singleLine="true" android:textColorHint="#bcbcbc" android:textSize="18sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="23dp" android:layout_marginBottom="23dp" android:background="#f3f3f3" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="手机号" android:textSize="16dp"/> <EditText android:id="@+id/et_registeractivity_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:background="@null" android:imeOptions="actionSearch" android:singleLine="true" android:hint="请输入手机号码" android:textColor="@color/black" android:textColorHint="#bcbcbc" android:textSize="18sp" tools:ignore="TextFields" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="23dp" android:layout_marginBottom="23dp" android:background="#f3f3f3" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/classes" android:textSize="16dp"/> <EditText android:id="@+id/et_registeractivity_classes" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:background="@null" android:imeOptions="actionSearch" android:singleLine="true" android:hint="请输入所在班级" android:textColor="@color/black" android:textColorHint="#bcbcbc" android:textSize="18sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="23dp" android:layout_marginBottom="23dp" android:background="#f3f3f3" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:textSize="16dp"/> <EditText android:id="@+id/et_registeractivity_password" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:background="@null" android:hint="请输入密码" android:imeOptions="actionSearch" android:singleLine="true" android:inputType="textPassword" android:textColor="@color/black" android:textColorHint="#bcbcbc" android:textSize="18sp" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="1dp" android:layout_marginTop="23dp" android:layout_marginBottom="23dp" android:background="#f3f3f3" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_marginRight="15dp" android:orientation="horizontal"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/et_registeractivity_phoneCodes" android:layout_width="196dp" android:layout_height="50dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_weight="3" android:hint="请输入验证码" android:textColor="#000000" android:textColorHint="#bcbcbc" /> <ImageView android:id="@+id/iv_registeractivity_showCode" android:layout_width="95dp" android:layout_height="63dp" android:layout_marginTop="-10dp" android:layout_weight="1.5" android:clickable="true" android:onClick="onClick" /> </LinearLayout> </LinearLayout> </LinearLayout> <Button android:id="@+id/bt_registeractivity_register" android:layout_width="match_parent" android:layout_height="55dp" android:layout_marginLeft="18dp" android:layout_marginTop="40dp" android:layout_marginRight="18dp" android:text="注册" android:textColor="@color/white" android:textSize="24sp" /> </LinearLayout> </RelativeLayout>
Java文件
package com.example.meiri; 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.Toast; import androidx.appcompat.app.AppCompatActivity; import com.example.meiri.db.DBOpenHelper; import com.example.meiri.util.Code; public class RegisterActivity extends AppCompatActivity implements View.OnClickListener { private String realCode; private DBOpenHelper mDBOpenHelper; private EditText mEtRegisteractivityID; //ID、学号 private Button mBtRegisteractivityRegister;//注册 private ImageView mIvRegisteractivityBack;//返回 private EditText mEtRegisteractivityUsername; //姓名 private EditText mEtRegisteractivityPhone; //手机号 private EditText mEtRegisteractivityclasses; //班级 private EditText mEtRegisteractivitypassword; //密码 private EditText mEtRegisteractivityPhonecodes;//用户输入的验证码 private ImageView mIvRegisteractivityShowcode; //验证码图片 @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() { mEtRegisteractivityID = findViewById(R.id.et_registeractivity_id); mEtRegisteractivityPhone = findViewById(R.id.et_registeractivity_phone); mEtRegisteractivityclasses = findViewById(R.id.et_registeractivity_classes); mBtRegisteractivityRegister = findViewById(R.id.bt_registeractivity_register); mIvRegisteractivityBack = findViewById(R.id.iv_registeractivity_back); mEtRegisteractivityUsername = findViewById(R.id.et_registeractivity_username); mEtRegisteractivityPhonecodes = findViewById(R.id.et_registeractivity_phoneCodes); mEtRegisteractivitypassword = findViewById(R.id.et_registeractivity_password); mIvRegisteractivityShowcode = findViewById(R.id.iv_registeractivity_showCode); 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, MainActivity.class); startActivity(intent1); finish(); break; case R.id.iv_registeractivity_showCode: //改变随机验证码的生成 mIvRegisteractivityShowcode.setImageBitmap(Code.getInstance().createBitmap()); realCode = Code.getInstance().getCode().toLowerCase(); break; // private String ID; //用户ID(学号) // private String name; //用户名,姓名 // private String phone; //手机号码 // private String classes; //用户单位,班级 // private String password; // 用户密码 case R.id.bt_registeractivity_register: //注册按钮 //获取用户输入的用户名、密码、验证码 String ID = mEtRegisteractivityID.getText().toString().trim(); String username = mEtRegisteractivityUsername.getText().toString().trim(); String phone = mEtRegisteractivityPhone.getText().toString().trim(); String classes = mEtRegisteractivityclasses.getText().toString().trim(); String password = mEtRegisteractivitypassword.getText().toString().trim(); String phoneCode = mEtRegisteractivityPhonecodes.getText().toString().toLowerCase(); //注册验证 if (!TextUtils.isEmpty(ID) && !TextUtils.isEmpty(username) && !TextUtils.isEmpty(phone) && !TextUtils.isEmpty(classes) && !TextUtils.isEmpty(password) && !TextUtils.isEmpty(phoneCode)) { if (phoneCode.equals(realCode)) { //将用户名和密码加入到数据库中 mDBOpenHelper.add(ID,username, phone,classes,password); 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; } } }
标签:总结,Toast,String,每日,private,2023,import,android,id From: https://www.cnblogs.com/JJTyyds/p/17170064.html