如何将主活动改为视图绑定,请给出代码改动,谢谢
package com.example.myapplication11;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.WindowDecorActionBar;
import android.annotation.SuppressLint;
import android.app.assist.AssistStructure;
import android.content.Intent;
import android.os.Bundle;
import android.service.controls.templates.ControlButton;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import com.example.myapplication11.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
private EditText nameText; // 修改为EditText
private EditText psdText; // 修改为EditText
private EditText repsdText; // 修改为EditText
private EditText cityText; // 修改为EditText
private AssistStructure.ViewNode maleBth;
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化EditText
nameText = findViewById(R.id.name);
psdText = findViewById(R.id.psd);
repsdText = findViewById(R.id.repsd);
cityText = findViewById(R.id.city);
}
@SuppressLint("RestrictedApi")
public void register(View view) {
String checkResult = checkInfo();
if (checkResult != null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("失败提示");
builder.setMessage(checkResult);
builder.setPositiveButton("确定", null);
builder.create().show();
} else {
String male = "男";
if (maleBth.isChecked()) {
male = "女";
}
Intent intent = new Intent(this, ResultActivity.class);
intent.putExtra("name", nameText.getText().toString());
intent.putExtra("city", cityText.getText().toString());
intent.putExtra("gender", male);
startActivity(intent);
}
}
@SuppressLint("RestrictedApi")
public String checkInfo() {
String name;
name = nameText.getText().toString();
if (name==null||"".equals(name)) {
return "用户名不能为空!";
}
String psd = psdText.getText().toString();
if (psd.length() < 6 || psd.length() > 15) {
return "密码位数应在6-15位之间";
}
String repsd = repsdText.getText().toString();
if (!psd.equals(repsd)) {
return "两次密码必须一致";
}
return null;
}
public void chooseCity(View view) {
Intent intent = new Intent(this, ChooseCityActivity.class);
startActivityForResult(intent, 0);
}
@SuppressLint("RestrictedApi")
protected void onActivityResult(int requestcode, int resultCode, Intent data) {
super.onActivityResult(requestcode, resultCode, data);
if (data != null) {
if (resultCode == 0x11) {
String city = data.getStringExtra("city");
cityText.setText(city);
}
}
}
}