2024.6.14
第七十三天
所花时间:2小时
代码量:400+
博客量:1
了解到的知识点:会议——登陆注册
package com.example.huiyi; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.example.huiyi.dao.userDao; import com.example.huiyi.entity.user; /** * function:连接页面加载首页 */ public class MainActivity extends AppCompatActivity { private static final String TAG = "mysql-party-MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void reg(View view){ startActivity(new Intent(getApplicationContext(),RegisterActivity.class)); } /** * function: 登录 * */ public void login(View view){ EditText EditTextAccount = findViewById(R.id.studentid); EditText EditTextPhone = findViewById(R.id.phone); new Thread(){ @Override public void run() { userDao userDao = new userDao(); int msg = userDao.login(EditTextAccount.getText().toString(),EditTextPhone.getText().toString()); SharedPreferences sharedPref = getSharedPreferences("app_prefs", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString("username", EditTextAccount.getText().toString()); editor.apply(); hand1.sendEmptyMessage(msg); if(msg == 1){ } } }.start(); } @SuppressLint("HandlerLeak") final Handler hand1 = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0){ Toast.makeText(getApplicationContext(), "登录失败", Toast.LENGTH_LONG).show(); } else if (msg.what == 1) { Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_LONG).show(); // 实验导入用户名 // 在用户登录成功后保存用户名到 SharedPreferences 中 // SharedPreferences sharedPreferences = getSharedPreferences("userData", MODE_PRIVATE); // SharedPreferences.Editor editor = sharedPreferences.edit(); // editor.putString("username",EditTextAccount.getText().toString() ); // editor.apply(); //试验一下!!!!!!!!!!!!!!!!!!!!!! startActivity(new Intent(getApplicationContext(),MeetingActivity.class)); } else if (msg.what == 2){ Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show(); } else if (msg.what == 3){ Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show(); } } }; }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:layout_editor_absoluteX="219dp" tools:layout_editor_absoluteY="207dp" android:padding="50dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" android:text="用户名:" /> <EditText android:id="@+id/studentid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="text" android:text="" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" android:text="电话:" /> <EditText android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="phone" android:text="" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> <Button android:layout_marginTop="50dp" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="登录" android:onClick="login" /> <Button android:id="@+id/button3" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="reg" android:text="注册" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>
package com.example.huiyi; import androidx.appcompat.app.AppCompatActivity; import android.annotation.SuppressLint; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.example.huiyi.dao.userDao; import com.example.huiyi.entity.user; /** * function:连接注册页面 */ public class RegisterActivity extends AppCompatActivity { private static final String TAG = "mysql-party-register"; EditText name = null; EditText sex = null; EditText department = null; EditText phone = null; EditText address = null; // @SuppressLint("MissingInflatedId") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); name = findViewById(R.id.name); sex = findViewById(R.id.sex); department = findViewById(R.id.department); phone = findViewById(R.id.phone); address = findViewById(R.id.address); } public void register(View view){ String name1 = name.getText().toString(); String sex1 = sex.getText().toString(); String department1 = department.getText().toString(); String phone1 = phone.getText().toString(); String address1 = address.getText().toString(); user user = new user(); user.setUserName(name1); user.setSex(sex1); user.setDepartment(department1); user.setPhone(phone1); user.setPosition(address1); new Thread(){ @Override public void run() { int msg = 0; userDao userDao = new userDao(); user uu = userDao.findStudent(user.getUserName()); if(uu != null){ msg = 1; } else{ boolean flag = userDao.register(user); if(flag){ msg = 2; } } hand.sendEmptyMessage(msg); } }.start(); } @SuppressLint("HandlerLeak") final Handler hand = new Handler() { public void handleMessage(Message msg) { if(msg.what == 0) { Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show(); } else if(msg.what == 1) { Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show(); } else if(msg.what == 2) { Toast.makeText(getApplicationContext(), "注册成功", Toast.LENGTH_LONG).show(); Intent intent = new Intent(); //将想要传递的数据用putExtra封装在intent中 intent.putExtra("a","注册"); setResult(RESULT_CANCELED,intent); startActivity(new Intent(getApplicationContext(),MainActivity.class)); finish(); } } }; }
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.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" tools:context=".RegisterActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:layout_editor_absoluteX="219dp" tools:layout_editor_absoluteY="207dp" android:padding="50dp" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" android:text="姓名:" /> <EditText android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="text" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" android:text="性别:" /> <EditText android:id="@+id/sex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textPersonName" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" android:text="部门:" /> <EditText android:id="@+id/department" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="textVisiblePassword" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" android:text="手机号码:" /> <EditText android:id="@+id/phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="text" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="15sp" android:text="家庭住址:" /> <EditText android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:ems="10" android:inputType="text" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> </LinearLayout> <Button android:layout_marginTop="50dp" android:id="@+id/button2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" android:onClick="register" /> </LinearLayout> </androidx.constraintlayout.widget.ConstraintLayout>

浙公网安备 33010602011771号