摘要:团队合作开发第一天,需要实现android端的注册登录功能,实现注册查重功能,登录账号密码匹配。
UI设计:
其中免费注册实现注册功能,点击进入注册界面,蓝色按钮实现登录功能(界面在之后进行优化)
代码:
import android.content.DialogInterface; import android.os.Bundle; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.navigation.NavController; import androidx.navigation.Navigation; import androidx.navigation.ui.AppBarConfiguration; import androidx.navigation.ui.NavigationUI; import com.google.android.material.bottomnavigation.BottomNavigationView; import com.lh.std_everything.ui.home.hometype.blog.WriteViewModel; public class MainActivity extends AppCompatActivity { private NavController controller; public static String getUserid() { return userid; } private static String userid; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); BottomNavigationView bottomNavigationView=findViewById(R.id.bottomNavigationView); controller = Navigation.findNavController(this,R.id.navhost); AppBarConfiguration configuration=new AppBarConfiguration.Builder(bottomNavigationView.getMenu()).build(); NavigationUI.setupActionBarWithNavController(this, controller,configuration); NavigationUI.setupWithNavController(bottomNavigationView, controller); userid=getIntent().getStringExtra("id"); }
public class Login extends AppCompatActivity { private static final String SHARED_FILE_NAME ="USERCOUNT" ; private AutoCompleteTextView search; private EditText pwd; private FloatingActionButton login_btn; private final static String requrl="http://39.98.165.112/STDEverything/login_servlet"; //ip:116.62.178.231 private TextView tip=null; // 要申请的权限 private String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CALL_PHONE, Manifest.permission.CAMERA,Manifest.permission.ACCESS_COARSE_LOCATION}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_login); getPower(); initView(); String[] arr = getUserCount(); ArrayAdapter<String> adapter= new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arr); search.setAdapter(adapter); tip.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(Login.this,Register.class)); } }); login_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final String name=search.getText().toString(); String userpwd=pwd.getText().toString(); final String reqdata="method=login&name="+name+"&pwd="+userpwd+""; new Thread(new Runnable() { @Override public void run() { try { final JSONObject jsonObject=new JSONObject(String.valueOf(HttpUtil.sendPost(requrl,reqdata)));//子线程获取json结果 runOnUiThread(new Runnable() { //主线程运行跳转活动 @Override public void run() { try { Toast.makeText(Login.this, jsonObject.get("msg").toString(), Toast.LENGTH_SHORT).show(); if(jsonObject.get("status").equals("1")){ //状态码1代表登陆成功 if(!judgeUserExists(name)) recordUser(name); startActivity(new Intent(Login.this, MainActivity.class) .putExtra("id",jsonObject.get("id").toString())); } } catch (JSONException e) { e.printStackTrace(); } } }); } catch (JSONException e) { e.printStackTrace(); } } }).start(); } }); } private void recordUser(String val){ //保存用户账户 Date date=new Date(); SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddhhmmssSSS"); SharedPreferences shf=getSharedPreferences(SHARED_FILE_NAME, Context.MODE_PRIVATE); SharedPreferences.Editor editor=shf.edit(); editor.putString(sdf.format(date),val); editor.apply(); }
public class Register extends AppCompatActivity { private RegisterHandler handler=null; private EditText name_text=null; private EditText pwd_text=null; private final static String requrl="http://39.98.165.112/STDEverything/login_servlet"; //ip:116.62.178.231 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_register); FloatingActionButton register_btn = findViewById(R.id.register_btn); name_text=findViewById(R.id.name); pwd_text=findViewById(R.id.pwd); handler=new RegisterHandler(this); register_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String name=name_text.getText().toString(); String pwd=pwd_text.getText().toString(); final String reqdata="method=login&name="+name+"&pwd="+pwd+""; new Thread(new Runnable() { @Override public void run() { try { JSONObject jsonObject=new JSONObject(String.valueOf(HttpUtil.sendPost(requrl,reqdata))); Message msg=new Message(); msg.what=100; msg.obj=jsonObject; handler.sendMessage(msg); } catch (JSONException e) { e.printStackTrace(); } } }).start(); } }); } static class RegisterHandler extends Handler{ private final WeakReference<Register> mcontext; RegisterHandler(Register context){ mcontext=new WeakReference<>(context); } @Override public void handleMessage(@NonNull Message msg) { super.handleMessage(msg); if(msg.what==100){ JSONObject jsonObject= (JSONObject) msg.obj; try { if(jsonObject.get("name").equals("yes")) Toast.makeText(mcontext.get(),"用户名已存在",Toast.LENGTH_SHORT).show(); else if(jsonObject.get("name").equals("no")){ new Thread(new Runnable() { @Override public void run() { String name=mcontext.get().name_text.getText().toString(); String pwd=mcontext.get().pwd_text.getText().toString(); final String reqdata="method=register&name="+name+"&pwd="+pwd+""; HttpUtil.sendPost(requrl,reqdata); sendEmptyMessage(888); } }).start(); } } catch (JSONException e) { e.printStackTrace(); } }else if(msg.what==888){ Toast.makeText(mcontext.get(), "注册成功", Toast.LENGTH_SHORT).show(); mcontext.get().startActivity(new Intent(mcontext.get(),Login.class)); } } } }
明天尝试将注册信息存入云服务器数据库。