[英]How do I smoothly store user input data with EditText and RadioButtons (with a color set as well)?
I want to store the data from both the RadioButtons as well as the EditText values in the User
attribute registeredData
, but I don't know how to access my function in a way that allows me to grab all the data, as well as displaying a color change from the RadioButtons. 我想将RadioButtons以及EditText值中的数据存储在
User
属性registeredData
,但是我不知道如何以允许我抓取所有数据以及显示RadioButtons的颜色变化。 Also how do I check if the input data is already in use (like email)? 另外,如何检查输入数据是否已被使用(例如电子邮件)?
I have tried splitting them into two different functions, but I can't get the data back from them into my User
attribute registeredData
. 我尝试将它们拆分为两个不同的函数,但是无法将它们的数据返回到我的
User
属性registeredData
。 This is my first try at coding an app so any help is appreciated. 这是我第一次尝试编写应用程序代码,因此对您的帮助表示赞赏。
public class Registration extends AppCompatActivity {
private EditText displayname, email, password, confirmpassword;
private Button bsubmit;
private RadioGroup rgroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
displayname = findViewById(R.id.displayname);
email = findViewById(R.id.useremail);
password = findViewById(R.id.password);
confirmpassword = findViewById(R.id.confirmpassword);
bsubmit = findViewById(R.id.bsubmit);
rgroup = findViewById(R.id.rgroupteams);
bsubmit.setOnClickListener(onRegister);
}
private View.OnClickListener onRegister = new View.OnClickListener() {
@Override
public void onClick(View v) {
final User registeredData;
registeredData = new User();
switch (v.getId()) {
case R.id.bsubmit:
String useremail = email.getText().toString();
String userdisplayname = displayname.getText().toString();
String userpassword = password.getText().toString();
registeredData.email = useremail;
registeredData.displayname = userdisplayname;
registeredData.password = userpassword;
case R.id.rgroupteams:
RadioGroup group = findViewById(R.id.rgroupteams);
group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int idOfSelected) {
switch (idOfSelected) {
case R.id.rbtnmantis:
bsubmit.setBackgroundColor(Color.parseColor("#FF0000"));
bsubmit.setTextColor(Color.parseColor("#000000"));
registeredData.team = "Mantis";
break;
case R.id.rbtlightbringers:
bsubmit.setBackgroundColor(Color.parseColor("#F4DC00"));
bsubmit.setTextColor(Color.parseColor("#000000"));
registeredData.team = "LightBringers";
break;
case R.id.rbtncryptographers:
bsubmit.setBackgroundColor(Color.parseColor("#1C00AA"));
bsubmit.setTextColor(Color.parseColor("#FFFFFF"));
registeredData.team = "Cryptographers";
break;
default:
registeredData.team = "";
}
}
});
}
}
};
}
Right now the color change only turns on after I've have clicked the submit button because I don't know how to better set my setOnClickListener()
, I haven't made use of any variables in the registeredData outside of this either, are they set up to be able to be accessed for some data (like displayname) to be displayed? 现在,颜色更改仅在单击提交按钮后才打开,因为我不知道如何更好地设置
setOnClickListener()
,我也没有使用registeredData中的任何变量。他们设置为可以访问某些要显示的数据(例如displayname)?
You need to move onCheckedChangeListener
outside of onClickListener
. 你需要移动
onCheckedChangeListener
之外onClickListener
。 Also, move registeredData
outside onClickListener. 另外,将
registeredData
移到onClickListener之外。 Make sure that registeredData is "globally" accessible. 确保registeredData是“全局”可访问的。 Then on button click and checked listener you can set data from input fields and from checkboxes to the object.
然后,单击按钮并选中侦听器,您可以从输入字段以及复选框到对象设置数据。
Second, with TextUtils.isEmpty(email.getText().toString()) you can get boolean if email is empty or not. 其次,如果电子邮件为空,则使用TextUtils.isEmpty(email.getText()。toString())可以获取布尔值。 This you can use for other input fields, just send text to the isEmpty method
您可以将其用于其他输入字段,只需将文本发送到isEmpty方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.