第5天 | |
所花时间(包括上课) | 2h |
代码量(行) | 140行 |
博客量(篇) | 1篇 |
学习到的知识点 | 页面的跳转,和注册页面,登录页面的构建 |
package com.example.myapplication1; 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.myapplication1.dao.UserDao; import com.example.myapplication1.entity.User; /** * function:连接注册页面 */ public class activity_register extends AppCompatActivity { private static final String TAG = "mysql-application1-register"; EditText userAccount = null; EditText userPassword = null; EditText userName = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_register); userAccount = findViewById(R.id.userAccount); userPassword = findViewById(R.id.userPassword); userName = findViewById(R.id.userName); } public void register(View view){ String userAccount1 = userAccount.getText().toString(); String userPassword1 = userPassword.getText().toString(); String userName1 = userName.getText().toString(); User user = new User(); user.setUserAccount(userAccount1); user.setUserPassword(userPassword1); user.setUserName(userName1); user.setUserType(1); user.setUserState(0); user.setUserDel(0); new Thread(){ @Override public void run() { int msg = 0; UserDao userDao = new UserDao(); User uu = userDao.findUser(user.getUserAccount()); 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); finish(); } } }; }
package com.example.myapplication1; 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.myapplication1.dao.UserDao; /** * function:连接页面加载首页 */ public class MainActivity extends AppCompatActivity { private static final String TAG = "mysql-application1-MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void reg(View view){ startActivity(new Intent(getApplicationContext(),activity_register.class)); } /** * function: 登录 * */ public void login(View view){ EditText EditTextAccount = findViewById(R.id.uesrAccount); EditText EditTextPassword = findViewById(R.id.userPassword); new Thread(){ @Override public void run() { UserDao userDao = new UserDao(); int msg = userDao.login(EditTextAccount.getText().toString(),EditTextPassword.getText().toString()); hand1.sendEmptyMessage(msg); } }.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(); } else if (msg.what == 2){ Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_LONG).show(); } else if (msg.what == 3){ Toast.makeText(getApplicationContext(), "账号不存在", Toast.LENGTH_LONG).show(); } } }; }
标签:Toast,12,2024.03,void,new,msg,import,android From: https://www.cnblogs.com/dmx-03/p/18067695