2024/3/13

今天开始写注册的后端代码:

package com.example.team;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.R;
import com.example.team.dao.UserDao;
import com.example.team.entity.User;

public class RegisterActivity extends AppCompatActivity {
private TextView m_text,text,textview;
EditText name = null;
EditText username = null;
EditText password = null;
EditText phone = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
m_text = findViewById(R.id.Text1);
text = findViewById(R.id.Text2);
textview = findViewById(R.id.textView1);
name = findViewById(R.id.name);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
phone = findViewById(R.id.phone);
Typeface typeface = Typeface.createFromAsset(getAssets(),"font/xiniu.ttf");
Typeface typeface2 = Typeface.createFromAsset(getAssets(), "font/momo.ttf");
textview.setTypeface(typeface);
m_text.setTypeface(typeface2);
text.setTypeface(typeface2);
name.setTypeface(typeface);
username.setTypeface(typeface);
phone.setTypeface(typeface);
password.setTypeface(typeface);

}


public void register(View view){
String cname = name.getText().toString();
String cusername = username.getText().toString();
String cpassword = password.getText().toString();
System.out.println(phone.getText().toString());
String cphone = phone.getText().toString();
User user = new User();

user.setName(cname);
user.setUsername(cusername);
user.setPassword(cpassword);
user.setPhone(cphone);

new Thread(){
@Override
public void run() {

int msg = 0;

UserDao userDao = new UserDao(RegisterActivity.this);

User uu = userDao.findUser(user.getName());

if(uu != null){
msg = 1;
}

boolean flag = userDao.register(user);
if(flag){
msg = 2;
}
hand.sendEmptyMessage(msg);

}
}.start();


}
final Handler hand = new Handler()
{
@Override
public void handleMessage(Message msg) {
if(msg.what == 0)
{
Toast.makeText(getApplicationContext(),"注册失败",Toast.LENGTH_LONG).show();

}
if(msg.what == 1)
{
Toast.makeText(getApplicationContext(),"该账号已经存在,请换一个账号",Toast.LENGTH_LONG).show();

}
if (msg.what == 2) {
Toast.makeText(getApplicationContext(),"注册成功",Toast.LENGTH_LONG).show();
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
};
}

posted @ 2024-03-13 18:01  芊羽鱼  阅读(30)  评论(0)    收藏  举报