设置密码的功能模块实现
package Home.login; 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.view.Window; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import com.example.expressdelivery.R; import Home.HttpUtil.HttpUtil; import com.google.android.material.floatingactionbutton.FloatingActionButton; import org.json.JSONException; import org.json.JSONObject; import java.lang.ref.WeakReference; public class SetPassword extends AppCompatActivity { private static final String TAG = "SetPassword"; private RegisterHandler handler=null; private EditText pwd_text=null; private EditText pwd2_text=null; CheckBox checkbox1,checkbox2; private String phone="",shishei=""; private static String requrl=""; //ip:116.62.178.231 @Override protected void onCreate(Bundle savedInstanceState) { Bundle bundle = this.getIntent().getExtras(); phone = bundle.getString("phone"); Log.d("获取到的name值为",phone); super.onCreate(savedInstanceState); supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_setpassword); initView(); FloatingActionButton register_btn = findViewById(R.id.register_btn); handler=new RegisterHandler(this); register_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String pwd=pwd_text.getText().toString(); String pwd2=pwd2_text.getText().toString(); if(!pwd.equals(pwd2)) { Toast.makeText(SetPassword.this, "两次密码输入不一致请重新输入", Toast.LENGTH_SHORT).show(); } final String reqdata="shishei="+shishei+"&username="+phone+"&password="+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(); } }); } private void initView() { pwd_text=findViewById(R.id.pwd); pwd2_text=findViewById(R.id.pwd2); checkbox1 = (CheckBox) findViewById(R.id.preview_checkbox1); checkbox2 = (CheckBox) findViewById(R.id.preview_checkbox2); //监听选中取消事件 checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked){ checkbox1.setChecked(true); shishei="教职工"; checkbox2.setChecked(false); }else { checkbox1.setChecked(false); shishei="学生"; } } }); checkbox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked){ checkbox2.setChecked(true); shishei="学生"; checkbox1.setChecked(false); }else { checkbox2.setChecked(false); shishei="教职工"; } } }); } static class RegisterHandler extends Handler { private final WeakReference<SetPassword> mcontext; RegisterHandler(SetPassword 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 username=mcontext.get().phone; requrl=""; Log.d(TAG, "onClick: phone==" + username); String pwd=mcontext.get().pwd_text.getText().toString(); String shishei=mcontext.get().shishei.toString(); final String reqdata="shishei="+shishei+"&username="+username+"&password="+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(),LoginActivity2.class)); } } } }