How to get the text from radio button in a radio group when radio button checked state is changed
Tag : java , By : ponchopilate
Date : March 29 2020, 07:55 AM
Does that help I have created two radio buttons in a radio group dynamically and one of them is checked. I need when i cheked another button then its value should be saved in string. But i have implemented checkedchangelistener for this.But its not working first time. Here is my code. , xml file <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RadioButton
android:id="@+id/radioMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_male"
android:checked="true" />
<RadioButton
android:id="@+id/radioFemale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/radio_female" />
</RadioGroup>
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/btn_display" />
</LinearLayout>
package com.mkyong.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MyAndroidAppActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
radioSexButton = (RadioButton) findViewById(selectedId);
Toast.makeText(MyAndroidAppActivity.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
|
Get radio button previous state Click event/Uncheck the radio if previously checked
Date : March 29 2020, 07:55 AM
I hope this helps . How can I see if a Radio button was already checked in a click event? , Keep a track of the previous state with temp variable: var prv;
var markIt = function(e) {
if (prv === this && this.checked) {
this.checked = false;
prv = null; //allow seemless selection for the same radio
} else {
prv = this;
}
};
$(function() {
$('input.radiooption').on('click', markIt);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type='radio' name='group' class='radiooption' />
<input type='radio' name='group' class='radiooption' />
<input type='radio' name='group' class='radiooption' />
<input type='radio' name='group' class='radiooption' />
<input type='radio' name='group' class='radiooption' />
|
how to un-check Radio Button with single click which is checked by default
Date : March 29 2020, 07:55 AM
help you fix your problem A JQuery solution, if you assing a class radioClass to your radio buttons: (function () {
$('.radioClass').on('mouseup', function (e) {
var xRadioB = this;
if ($(xRadioB).is(':checked')) {
setTimeout(function () {
$(xRadioB).prop('checked', false);
}, 5);
}
});
})();
|
angular material mat-radio-button default checked not working after adding name on mat-radio-group
Date : March 29 2020, 07:55 AM
it helps some times You will need the name="your_name" near [(ngModel)], then the checked property will not work but you can do like this. <form>
<mat-radio-group [(ngModel)]="radioOptions" name="radioButtons">
<mat-radio-button [value]="'TEST1'">TEST1</mat-radio-button>
<mat-radio-button [value]="'TEST2'">TEST2</mat-radio-button>
</mat-radio-group> </form>
import { FormsModule } from '@angular/forms';
export class ConfigurazioneComponent implements OnInit {
radioOptions: string = 'TEST1';
}
|
How will i set default radio button checked and know which radio button is checked from the radio-group in a reactive-fo
Date : March 29 2020, 07:55 AM
it helps some times Initilize the form control with the value of whatever radio button you want to be checked as default. There is no need of adding a default checked attribute in the any of the radio buttons this.form = this._fb.group({
selectedOption: ["1", Validators.required], // this will make the second radio button checked as default.
});
this.form.get('selectedOption').value
<input (change)="foo()" id="radio-button-1" class="bx--radio-button" type="radio" formControlName="selectedOption" value="0">
// inside the method foo() check for the currently selected radio button
|