取消选中“单击按钮”时的radiobutton / radiogroup-Android [英] uncheck radiobutton/radiogroup on Button click - Android
问题描述
我在无线电组中使用单选按钮,并且每个无线电组是动态生成的。我希望所有未选中的单选按钮组都更具体,我希望用户单击清除按钮时,不选中单选按钮组中的所有单选按钮。 我在列表视图中使用radiogroup并使用自定义适配器生成每个列表视图。
这是我的代码:
维护活动
公共类MainActivity扩展了AppCompatActivity {
私有ListView simpleListView;
私人CustomAdapter customAdapter;
private String []问题;
私人Button提交,清除;
私人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);
个问题= 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);
提交=(按钮)footerView.findViewById(R.id.submit1);
simpleListView.addFooterView(footerView);
View headerView = getLayoutInflater()。inflate(R.layout.header,null);
simpleListView.addHeaderView(headerView);
customAdapter = new CustomAdapter(getApplicationContext(),问题);
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){
字符串消息= ;
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;
中断;
}
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 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);
返回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);
返回true;
}
if(id == R.id.admin){
Intent inent = new Intent(MainActivity.this,adminactivity.class);
startActivity(inent);
返回true;
}
if(id == R.id.SerialPort){
Intent inent = new Intent(MainActivity.this,DeviceListActivity.class);
startActivity(inent);
返回true;
}
if(id == R.id.customservice){
Intent inent = new Intent(MainActivity.this,testactivity_main.class);
startActivity(inent);
返回true;
}
if(id == R.id.exit){
ActivityCompat.finishAffinity(MainActivity.this);
返回true;
}
return super.onOptionsItemSelected(item);
}
自定义适配器
公共类CustomAdapter扩展了BaseAdapter {
上下文上下文;
String [] questionList;
LayoutInflater过滤器;
public ArrayList< String> selectedAnswers;
public CustomAdapter(Context applicationContext,String [] questionList){
this.context = context;
this.questionsList = questionsList;
selectedAnswers = new ArrayList<>();
for(int i = 0; i selectedAnswers.add( 3);
}
inflter =(LayoutInflater.from(applicationContext));
}
@Override
public int getCount(){
returnquestionsList.length;
}
@Override
public Object getItem(int i){
return issuesList [i];
}
@Override
public long getItemId(int i){
return i;
}
@Override
public int getViewTypeCount(){
returnquestionsList.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问题=(TextView)view.findViewById(R.id.question);
question.setText(questionsList [i]);
//初始化/恢复UI单选按钮状态
final RadioGroup rg =(RadioGroup)view.findViewById(R.id.radio_group);
最终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);
}否则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 {
//没有答案。
rb_yes.setBackgroundColor(Color.GRAY);
rb_no.setBackgroundColor(Color.GRAY);
}
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(RadioGroup group,intcheckedId){
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);
休息;
情况下R.id.no:
rb_yes.setBackgroundColor(Color.BLACK);
rb_no.setBackgroundColor(Color.rgb(255,165,0));
selectedAnswers.set(i, 2);
休息;
}
}
});
返回视图;
}
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>
<按钮
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
/>
<按钮
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 =清除
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->
< 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
工具: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 =否
android:textColor = @ color / White
android:textSize = 50dp />
< / RadioGroup>
< / FrameLayout>
我不知道在哪里我错了
clear.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
}
});
谢谢您
RadioGroup radiogrp =(RadioGroup)findViewById(R.id.radiogrp); / div> 使用它。
内部点击
radiogrp.clearCheck();
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
Use this..
RadioGroup radiogrp = (RadioGroup)findViewById(R.id.radiogrp);
Inside onclick
radiogrp.clearCheck();
这篇关于取消选中“单击按钮”时的radiobutton / radiogroup-Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!