i am now develop an app,when it comes to register progress, i need to set a listener in a register button to send the information to the server,at the same time, i want to set a listener in a sex choice button which is used RadioGroup.when i set the listener which is for radiogroup in a listener for regist,it seems the radiogroup listener did not work properly.i am wonder why.here is the code
findViewById(R.id.rigister_btn_register).setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
EditText accountEt=(EditText) findViewById(R.id.register_account);
EditText passwordEt=(EditText) findViewById(R.id.register_password);
EditText nickEt=(EditText) findViewById(R.id.register_nick);
/*RadioGroup group = (RadioGroup)findViewById(R.id.register_radiogroup);
RadioButton maleButton=(RadioButton)findViewById(R.id.register_radio_nan);
maleButton.setChecked(true);
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group,int id) {
RadioButton femaleButton=(RadioButton)findViewById(R.id.register_radio_nv);
if(id==femaleButton.getId()){
sex="female";
}
}
});*/
btw,the var "sex" has been initialized at the beginning of the class which is male.
Related
Working on an education android app. One provides answers through EditText input, radio buttons and CheckBox.
I want a check for the RadioGroup that when no RadioButton is clicked it will prompt the student and prevent submission. I succeeded in EditText but unsucessful for RadioButton.
public void checkEmptyButtons(View view){
RadioGroup rg = (RadioGroup) findViewById(R.id.myRg);
if (rg.getCheckedRadioButton()==-1{
// this is where I have a problem
//I don't even know how to search on the //dev.anderoid site
}
}
That's where am stucked. Thanks in advance
.
you need radioGroup checked Change Listener:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//here you will get callback everytime radio button is checked along with id. You can have boolean checked here to know if any of your radio button is checked
}
});
Use getCheckedRadioButtonId() in stead of getCheckedRadioButton().
if (rg.getCheckedRadioButtonId() == -1) {
//no radio buttons are checked
}
else {
//any one of the radio buttons is checked
}
I have a simple program that has 3 radio buttons in a group. When I click on any of the radio buttons I have a Toast appear to let the user know which button they have selected.
My problem I am facing is I have a onClick event for a button that clearCheck the radio group. When that happens, the Toast message appears again with the previous selected information from the radio button.
How do I prevent the toast from happening when I clearCheck the radio group?
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize Radio Group and attach click handler
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.clearCheck();
// Attach CheckedChangeListener to Radio Group
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
if(null!=rb && checkedId > -1){
Toast.makeText(MainActivity.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
public void onClear(View v){
// Clears the radio buttons when the clear button is pressed
radioGroup.clearCheck();
}
That is because clearing the check is changing the checked status of the radio group. You need to set a flag to let the listening know it should ignore that change.
Just a simple boolean declared in the class, flagged in onClear and checked in onCheckChanged (and reset the flag in onCheckChanged also).
I have a ListView of questions. In each row there is a question and a RadioGroup with possibile answers. Not all the questions have the same number of answers, so I add the RadioButtons dynamically in my custom ListAdapter. My question is how to add Listener to these dynamically created RadioButtons. My code:
RadioGroup rg = (RadioGroup) convertView.findViewById(R.id.question);
rg.removeAllViews();
for (String qA : question.answers) {
RadioButton rb = new RadioButton(getContext());
rb.setText(qA.toString());
rg.addView(rb);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
//WHAT SHOULD I PUT HERE?
}
}
});
Maby something like this:
#Override public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton btn = (RadioButton)findViewById(checkedId);
}
and then something with the btn? :)
To find the checked button
View checkedButton = rg.findViewById(checkedId);
To find the index of checked button
int checkedIndex = rg.indexOfChild(checkedButton);
Before doing the above you have to set id to the buttons like mentioned in the comments rb.setId(count). Count has to be unique for each button within a group.
1 first create global variable of CompoundButton.OnCheckedChangeListener in your adapter for example private CompoundButton.OnCheckedChangeListener listener
2 second you have to attach listener variable with your radio button at the time of creating the radio button.
3 third when you bind the data or value with your ui you have to call this method
listener = new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged ( CompoundButton buttonView, boolean isChecked )
{
}
};
I have created a layout xml file where I have two RadioButtons.
By default, RadioButton 1 is selected and I am displaying a DatePicker component on the screen but when the user selects RadioButton2 the DatePicker should disappear from the screen.
How can I handle the scenario? Should I make the change in layout / Java code?
It is actually very simple.
Get a reference of your RadioGroup and your DatePicker. Implement an OnCheckedChangeListener for the RadioGroup and check which RadioButton was checked in there.
If RadioButton A was checked set the visibility on your DatePicker to visible and if RadioButton B was checked set the visibility to gone or invisible depending on your requirement.
As an example.
public class MyActivity extends Activity {
private RadioGroup choice;
private DatePicker datePicker;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.your_layout);
choice = (RadioGroup) findViewById(R.id.choice);
datePicker = (DatePicker) findViewById(R.id.date_picker);
choice.setOnCheckedChangeListener(
new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch(checkedId) {
case R.id.radio_button_a:
datePicker.setVisibility(View.VISIBLE);
break;
case R.id.radio_button_b:
datePicker.setVisibility(View.GONE);
break;
}
}
});
}
}
In theory it should be something like that.
I'm creating buttons dynamically ...
for(int i=0; i<colSize;i++){
final Button btn = new Button(this);
btn.setText(SectionName[i]);
btn.setTextSize(10);
btn.setPadding(8, 3,8, 3);
btn.setTextColor(Color.WHITE);
btn.setTypeface(Typeface.SERIF, Typeface.BOLD);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//***Every time that I click my button is selected !:)
btn.setSelected(true);
}
});
}
But how could I deselect the other buttons that were selected, I just want one Button selected! :)
The brutal way (works if you have few buttons) - save your button references and create private method which loops through your buttons and deselects once you don't need
Extend your button class and make it listen for custom event which is generated when one of the buttons is clicked
Look at the RadioGroup implementation
Variation of #1. Instead of creating separate listeners for your buttons create just one and reuse it for all buttons. Extend that listener from OnClickListener and add List field. Each time you assign listener to the button add button reference to that list. Now, when onClick is triggered simply loop through the list and disable "other" buttons
Declare a variable to store the Id of the Clicked Button ::
private int EnabledButton;
set an ID on every button when are created ::
btn.setId(i);
or a tag ::
btn.setTag(i);
then in that Listener get the "EnabledButton", and call a function to deselect the other buttons::
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EnabledButton=btn.getId();
DeselectButtons();
btn.setSelected(true);
}
});
The Function to deselect the other Buttons ::
public void DeselectButtons() {
for(int i=0; i<NumberofButtons;i++){
if (EnabledButton!= i)
this.findViewById(i).setSelected(false);
}
}