我遇到RadioButton
的问题。我试图传递RadioButton
的值,但对我不起作用。
即使button.setSelected(true)
也无效。
活动1:
private EditText FirstName;
private EditText LastName;
private EditText MobileNo;
private Button BtnNext;
private ProgressBar progressBar;
private String selectedtype="";
private RadioGroup Gender_grp;
private RadioButton btn_gender;
private RadioButton btn_gender1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_form);
Firebase.setAndroidContext(this);
FirstName=(EditText)findViewById(R.id.firstname);
LastName=(EditText)findViewById(R.id.lastname);
Gender_grp=(RadioGroup) findViewById(R.id.radio_group);
btn_gender=(RadioButton)findViewById(R.id.btn_male);
btn_gender1=(RadioButton)findViewById(R.id.btn_female);
MobileNo=(EditText)findViewById(R.id.mobile);
BtnNext=(Button)findViewById(R.id.btn_regform);
progressBar = (ProgressBar) findViewById(R.id.progressBar_form);
btn_gender.setSelected(true);
Gender_grp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged(RadioGroup group, int checkedId)
{
if(checkedId==R.id.btn_male)
{
selectedtype=btn_gender.getText().toString();
}
else if(checkedId==R.id.btn_female)
{
selectedtype=btn_gender1.getText().toString();
}
}
});
BtnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
Firebase ref=new Firebase(config_firebaseurl.FIREBASE_URL);
String fname=FirstName.getText().toString().trim();
String lname= LastName.getText().toString().trim();
String phno=MobileNo.getText().toString().trim();
Toast.makeText(getApplicationContext(),"Information Added", Toast.LENGTH_LONG).show();
Intent i=new Intent(registration_form_handler.this, means_of_transport_handler.class);
i.putExtra("fname", fname);
i.putExtra("lname", lname);
i.putExtra("sex", selectedtype);
i.putExtra("phno", phno);
startActivity(i);
progressBar.setVisibility(View.GONE);
}
});
}
}
活动2:
String getfname=getIntent().getStringExtra("fname");
String getlname=getIntent().getStringExtra("lname");
String getgender=getIntent().getStringExtra("sex");
String getphno=getIntent().getStringExtra("phno");
String license_no = licenseno.getText().toString().trim();
transport.setFirstName(getfname);
transport.setLastName(getlname);
transport.setGender(getgender);
transport.setmobileNo(getphno);
ref.child("means_of_transport").push().setValue(transport);
Toast.makeText(getApplicationContext(), "Information Added",Toast.LENGTH_LONG).show();
在数据库中,结果为:
gender=""
可以选择RadioButton
,但我一次只能选择一个。{/ p>
XML文件:
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:id="@+id/radio_group">
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male"
android:checked="false"
android:id="@+id/btn_male"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:checked="false"
android:id="@+id/btn_female" />
</android.support.design.widget.TextInputLayout>
</RadioGroup>
答案 0 :(得分:0)
您的RadioButton
位于群组内,尝试从RadioGroup
获取所选的ID。
解决的代码:
private EditText FirstName;
private EditText LastName;
private EditText MobileNo;
private Button BtnNext;
private ProgressBar progressBar;
private String selectedtype="";
private RadioGroup Gender_grp;
private RadioButton btn_gender;
private RadioButton btn_gender1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_form);
Firebase.setAndroidContext(this);
FirstName=(EditText)findViewById(R.id.firstname);
LastName=(EditText)findViewById(R.id.lastname);
Gender_grp=(RadioGroup) findViewById(R.id.radio_group);
btn_gender=(RadioButton)findViewById(R.id.btn_male);
btn_gender1=(RadioButton)findViewById(R.id.btn_female);
MobileNo=(EditText)findViewById(R.id.mobile);
BtnNext=(Button)findViewById(R.id.btn_regform);
progressBar = (ProgressBar) findViewById(R.id.progressBar_form);
btn_gender.setSelected(true);
BtnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//code changes here
int id = Gender_grp.getCheckedRadioButtonId();
Gender_grp = (RadioButton) findViewById(id);
selectedtype = Gender_grp.getText().toString();
progressBar.setVisibility(View.VISIBLE);
Firebase ref=new Firebase(config_firebaseurl.FIREBASE_URL);
String fname=FirstName.getText().toString().trim();
String lname= LastName.getText().toString().trim();
String phno=MobileNo.getText().toString().trim();
Toast.makeText(getApplicationContext(),"Information Added", Toast.LENGTH_LONG).show();
Intent i=new Intent(registration_form_handler.this, means_of_transport_handler.class);
i.putExtra("fname", fname);
i.putExtra("lname", lname);
i.putExtra("sex", selectedtype);
i.putExtra("phno", phno);
startActivity(i);
progressBar.setVisibility(View.GONE);
}
});
}
}
答案 1 :(得分:0)
private EditText FirstName;
private EditText LastName;
private EditText MobileNo;
private Button BtnNext;
private ProgressBar progressBar;
private String selectedtype="";
private RadioGroup Gender_grp;
private RadioButton radioBtn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registration_form);
Firebase.setAndroidContext(this);
FirstName=(EditText)findViewById(R.id.firstname);
LastName=(EditText)findViewById(R.id.lastname);
Gender_grp=(RadioGroup) findViewById(R.id.radio_group);
MobileNo=(EditText)findViewById(R.id.mobile);
BtnNext=(Button)findViewById(R.id.btn_regform);
progressBar = (ProgressBar) findViewById(R.id.progressBar_form);
btn_gender.setSelected(true);
BtnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
progressBar.setVisibility(View.VISIBLE);
Firebase ref=new Firebase(config_firebaseurl.FIREBASE_URL);
// get selected radio button from radioGroup
int radioBtnId = Gender_grp..getCheckedRadioButtonId();
// find the radiobutton by returned id
radioBtn = (RadioButton) findViewById(selectedId);
selectedtype = radioBtn.getText().toString().trim();
String fname=FirstName.getText().toString().trim();
String lname= LastName.getText().toString().trim();
String phno=MobileNo.getText().toString().trim();
Toast.makeText(getApplicationContext(),"Information Added", Toast.LENGTH_LONG).show();
Intent i=new Intent(registration_form_handler.this, means_of_transport_handler.class);
i.putExtra("fname", fname);
i.putExtra("lname", lname);
i.putExtra("sex", selectedtype);
i.putExtra("phno", phno);
startActivity(i);
progressBar.setVisibility(View.GONE);
}
});
}
}
在完成其他活动时获取数据。
注意:任何变量都将以小后者声明为start。