我在无线电组中使用单选按钮,并且每个无线电组是动态生成的。我希望所有未选中的单选按钮组都更具体,我希望用户单击清除按钮时,不选中单选按钮组中的所有单选按钮。 我在列表视图中使用radiogroup,并使用自定义适配器生成每个列表视图。
这是我的代码:
主要活动
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);
}
自定义适配器
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>
我不知道我要去哪里。
clear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
谢谢
答案 0 :(得分:0)
使用它。
RadioGroup radiogrp = (RadioGroup)findViewById(R.id.radiogrp);
内部点击
radiogrp.clearCheck();
答案 1 :(得分:0)
对于像这样的简单任务,最好的方法不需要像jQuery这样的完整JavaScript库。
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>