问题
I am using radiobuttons in my radiogroup and each radiogroup is generated dynamically. I want all radiogroup unchecked in more specific I want all radiobuttons in radiogroup should be unchecked when user click on clear button. I using radiogroup in my listview and generating each listview using a custom adapter.
Here is my code:
Mainactivity
public class MainActivity extends AppCompatActivity {
private ListView simpleListView;
private CustomAdapter customAdapter;
private String[] questions;
private Button submit, clear;
private RadioGroup radioGroup;
FileOutputStream fstream;
private boolean forceClear;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
questions = getResources().getStringArray(R.array.questions);
simpleListView = (ListView) findViewById(R.id.simpleListView);
View Listview = getLayoutInflater().inflate(R.layout.list_items,null);
RadioButton rb_yes = Listview.findViewById(R.id.yes);
RadioButton rb_no = Listview.findViewById(R.id.no);
final RadioGroup radioGroup= Listview.findViewById(R.id.radio_group);
View footerView = getLayoutInflater().inflate(R.layout.footer,null);
clear= (Button) footerView.findViewById(R.id.clear);
submit = (Button) footerView.findViewById(R.id.submit1);
simpleListView.addFooterView(footerView);
View headerView = getLayoutInflater().inflate(R.layout.header, null);
simpleListView.addHeaderView(headerView);
customAdapter = new CustomAdapter(getApplicationContext() , questions);
simpleListView.setAdapter(customAdapter);
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
radioGroup.check(-1);
radioGroup.clearCheck();
Toast.makeText(getApplicationContext() , "please answer" , Toast.LENGTH_SHORT).show();
for (int i = 0; i < radioGroup.getChildCount(); i++) {
RadioButton radioButton = (RadioButton) radioGroup.getChildAt(i);
radioButton.setChecked(false);
}
}});
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String message = "";
boolean found_unanswered = false;
if(customAdapter != null){
for(int i = 0 ; i<customAdapter.getSelectedAnswers().size() ; i++){
if(customAdapter.getSelectedAnswers().get(i).equals("3")){
Toast.makeText(getApplicationContext() , "please answer" , Toast.LENGTH_SHORT).show();
found_unanswered = true;
break;
}
message = message + "\n" + (i + 1) + " " + customAdapter.getSelectedAnswers().get(i);
}
}
if(!found_unanswered){
try {
fstream = openFileOutput("user_answer", Context.MODE_PRIVATE);
fstream.write(message.getBytes());
fstream.close();
Toast.makeText(getApplicationContext() ,message , Toast.LENGTH_LONG).show();
Intent inent = new Intent(view.getContext(), DetailsActivity.class);
startActivity(inent);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
public void clearButtonClicked (View view){
View Listview = getLayoutInflater().inflate(R.layout.list_items,null);
RadioGroup radioGroup= Listview.findViewById(R.id.radio_group);
radioGroup.clearCheck();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.about) {
Intent inent = new Intent(MainActivity.this, devicedetailsActivity.class);
startActivity(inent);
return true;
}
if (id == R.id.admin) {
Intent inent = new Intent(MainActivity.this, adminactivity.class);
startActivity(inent);
return true;
}
if (id == R.id.SerialPort) {
Intent inent = new Intent(MainActivity.this, DeviceListActivity.class);
startActivity(inent);
return true;
}
if (id == R.id.customservice) {
Intent inent = new Intent(MainActivity.this, testactivity_main.class);
startActivity(inent);
return true;
}
if (id == R.id.exit) {
ActivityCompat.finishAffinity(MainActivity.this);
return true;
}
return super.onOptionsItemSelected(item);
}
Custom Adapter
public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
LayoutInflater inflter;
public ArrayList<String> selectedAnswers;
public CustomAdapter(Context applicationContext, String[] questionsList) {
this.context = context;
this.questionsList = questionsList;
selectedAnswers = new ArrayList<>();
for (int i = 0; i < questionsList.length; i++) {
selectedAnswers.add("3");
}
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return questionsList.length;
}
@Override
public Object getItem(int i) {
return questionsList[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public int getViewTypeCount() {
return questionsList.length;
}
@Override
public int getItemViewType(int i) {
return i;
}
@Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
View view = convertView;
if (convertView == null) {
if (inflter != null) {
view = inflter.inflate(R.layout.list_items, null);
}
}
TextView question = (TextView) view.findViewById(R.id.question);
question.setText(questionsList[i]);
// initialize/ restore UI Radio Button State
final RadioGroup rg = (RadioGroup) view.findViewById(R.id.radio_group);
final RadioButton rb_yes = (RadioButton) rg.findViewById(R.id.yes);
final RadioButton rb_no = (RadioButton) rg.findViewById(R.id.no);
if(selectedAnswers.get(i).equals("1")){
rg.check(R.id.yes);
rb_yes.setBackgroundColor(Color.GREEN);
rb_no.setBackgroundColor(Color.BLACK);
}else if(selectedAnswers.get(i).equals("2")){
rg.check(R.id.no);
rb_yes.setBackgroundColor(Color.BLACK);
rb_no.setBackgroundColor(Color.rgb(255,165,0));
}else{
// no answer.
rb_yes.setBackgroundColor(Color.GRAY);
rb_no.setBackgroundColor(Color.GRAY);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb_yes = (RadioButton) group.findViewById(R.id.yes);
RadioButton rb_no = (RadioButton) group.findViewById(R.id.no);
switch (checkedId){
case R.id.yes:
rb_yes.setBackgroundColor(Color.GREEN);
rb_no.setBackgroundColor(Color.BLACK);
selectedAnswers.set(i, "1");
break;
case R.id.no:
rb_yes.setBackgroundColor(Color.BLACK);
rb_no.setBackgroundColor(Color.rgb(255,165,0));
selectedAnswers.set(i, "2");
break;
}
}
});
return view;
}
public List<String> getSelectedAnswers(){
return selectedAnswers;
}
Footer.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp">
<Button
android:id="@+id/submit1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="0.5dp"
android:background="@drawable/round_button"
android:text="Submit"
android:textColor="@color/White"
android:textSize="30sp"
/>
<Button
android:id="@+id/clear"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_margin="0.5dp"
android:background="@drawable/round_button"
android:text="Clear"
android:textColor="@color/White"
android:textSize="30sp"
/>
Listitem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/White">
<!-- TextView for displaying question-->
<TextView
android:id="@+id/question"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="@dimen/activity_horizontal_margin"
android:textColor="#000"
android:textSize="30dp"
/>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/main">
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:background="@color/Black"
android:button="@null"
android:paddingHorizontal="30dp"
android:paddingVertical="5dp"
android:text="YES"
android:textColor="@color/White"
android:textSize="50dp" />
<RadioButton
android:id="@+id/no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="@color/Black"
android:button="@null"
android:paddingHorizontal="30dp"
android:paddingVertical="5dp"
android:text="NO"
android:textColor="@color/White"
android:textSize="50dp" />
</RadioGroup>
</FrameLayout>
I don't know where I am going wrong.
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
Thank you
回答1:
Use this..
RadioGroup radiogrp = (RadioGroup)findViewById(R.id.radiogrp);
Inside onclick
radiogrp.clearCheck();
回答2:
The best approach for a simple task like this does not needs a full JavaScript library like jQuery.
document.getElementById("main2").addEventListener("click", function()
{
document.getElementById("subCheckButtons").style.opacity = 1;
}, false);
document.getElementById("main1").addEventListener("click", function()
{
document.getElementById("subCheckButtons").style.opacity = 0;
document.getElementById("sub1").checked = false;
document.getElementById("sub2").checked = false;
}, false);
<input type="radio" id="main1" name="main" />
<input type="radio" id="main2" name="main" />
<div id="subCheckButtons" style="opacity:0;">
<input type="radio" id="sub1" name="sub" class="subCheck" />
<input type="radio" id="sub2" name="sub" class="subCheck" />
</div>
来源:https://stackoverflow.com/questions/51236639/uncheck-radiobutton-radiogroup-on-button-click-android