我一直试图只保留其中一个单选按钮,但每次用户都可以选择多个单选按钮。
如何只保留一个或当前选中的单选按钮?
我基本上有一个TableLayout
,它有一个TableRow
作为XML布局,从代码中发挥作用。此外,在RadioButton上应用了onClickListener
,它为当前选定的单选按钮提取tag
。
有人可以帮我解决这个问题吗?
对于参考。我的Table Row的XML布局:
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tblDynStoreRow"
android:layout_margin="0.0dp"
android:layout_width="match_parent" >
<TextView
android:id="@+id/tvStoreCount"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="12dp"
android:singleLine="true"
android:scrollHorizontally="true" />
<RadioButton
android:id="@+id/rb1StoreName"
android:textSize="10sp"
android:layout_width="wrap_content"
android:layout_height="31dp"
android:singleLine="true"
android:scrollHorizontally="true" />
</TableRow>
onCreate()方法:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store_selection);
TableLayout tl = (TableLayout) findViewById(R.id.mainstoretable);
//RadioButton rb1;
TextView TVdate = (TextView) findViewById(R.id.textView1_date2);
Intent getStorei = getIntent();
// UsrGPSLock = i.getStringExtra("currUsrLat") + "," +
// i.getStringExtra("currUsrLon");
uuid = getStorei.getStringExtra("imei").trim();
userDate = getStorei.getStringExtra("userDate");
TVdate.setText(userDate);
/*TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
uuid = tManager.getDeviceId();*/
Button btnStart = (Button)findViewById(R.id.startQues);
dbengine.open();
storeList = dbengine.FetchStoreList();
dbengine.close();
/*final String[] storeCode = new String[storeList.length];
final String[] storeName = new String[storeList.length];*/
storeCode = new String[storeList.length];
storeName = new String[storeList.length];
for (int splitval = 0; splitval <= (storeList.length - 1); splitval++) {
StringTokenizer tokens = new StringTokenizer(String.valueOf(storeList[splitval]), "_");
storeCode[splitval] = tokens.nextToken().trim();
storeName[splitval] = tokens.nextToken().trim();
}
System.out.println(storeList);
// Get the TableLayout
/*TableLayout tl = (TableLayout) findViewById(R.id.maintable);*/
LayoutInflater inflater = getLayoutInflater();
// Go through each item in the array
for (int current = 0; current <= (storeList.length - 1); current++) {
TableRow storeRow = (TableRow)inflater.inflate(R.layout.store_table_row, tl, false);
TextView tv1 = (TextView)storeRow.findViewById(R.id.tvStoreCount);
final RadioButton rb1 = (RadioButton)storeRow.findViewById(R.id.rb1StoreName);
//tv1.setTag(current);
tv1.setText((""+(current+1)).trim());
//tv1.setId();
rb1.setTag(storeCode[current]);
rb1.setText(storeName[current]);
tl.addView(storeRow);
/*// Create a TableRow and give it an ID
tr = new TableRow(this);
tr.setId(200 + current);
tr.setTag(storeCode[current]);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tr.setClickable(true);
rb1 = new RadioButton(this);
rb1.setId(current);
rb1.setTag(storeCode[current]);
rb1.setText(storeName[current]);
rb1.setTextColor(Color.BLACK);
rb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tr.addView(rb1);
CheckBox cb1 = new CheckBox(this);
cb1.setId(500 + current);
// cb1.setText(provinces[current]);
cb1.setTextColor(Color.BLACK);
cb1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tr.addView(cb1);
* CheckBox cb2 = new CheckBox(this); cb2.setId(300+current);
* //cb1.setText(provinces[current]); cb2.setTextColor(Color.BLACK);
* cb2.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,
* LayoutParams.WRAP_CONTENT)); tr.addView(cb2);
// Add the TableRow to the TableLayout
tl.addView(tr, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));*/
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
System.out.println("inside-onClick");
//System.out.println(arg0.getId());
System.out.println(arg0.getTag());
selStoreID = arg0.getTag().toString();
if(prevSel == 0) {
prevSelStoreID = selStoreID;
prevID = arg0.getId();
prevSel = 1;
}
else{
}
/*rb1.refreshDrawableState();*/
if(prevSelStoreID.equals(arg0.getTag().toString())){
/*int rbID;
rbID = rb1.getId();
System.out.println("RB ID: " + rbID);*/
}
else{
int rbID;
rbID = arg0.getId();
//rb1.refreshDrawableState();
//rb1.clearFocus();
//rb1.findViewById(rbID).isSelected();
rb1.findViewById(prevID).clearFocus();
rb1.findViewById(prevID).invalidate();
rb1.findViewById(prevID).refreshDrawableState();
System.out.println("prevID: " + prevID);
System.out.println("rbID: " + rbID);
rb1.setChecked(false);
//rb1.findViewWithTag(prevSelStoreID).invalidate();
prevSel = 0;
prevSelStoreID = selStoreID;
prevID = rbID;
rb1.findViewById(rbID).isSelected();
}
}
});
}
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent nxtP3 = new Intent(StoreSelection.this, LastVisitDetails.class);
nxtP3.putExtra("storeID", selStoreID);
nxtP3.putExtra("imei", uuid);
nxtP3.putExtra("date", userDate);
startActivity(nxtP3);
}
});
}
答案 0 :(得分:2)
为了防止多次检查RadioButton,它们必须都在同一个RadioGroup中。
要么在XML中定义一个Radiogroup(但是从我看到你试图做的那个将是困难的)
或者您使用Java创建它:RadioGroup rg = new RadioGroup(this);
编辑编辑
试试这个......希望它有效
RadioGroup rg = new RadioGroup(this);
RadioButton[] rb = new RadioButton[storeList.length];
for (int current = 0; current <= (storeList.length - 1); current++) {
TableRow storeRow = (TableRow)inflater.inflate(R.layout.store_table_row, tl, false);
TextView tv1 = (TextView)storeRow.findViewById(R.id.tvStoreCount);
rb[current] = (RadioButton)storeRow.findViewById(R.id.rb1StoreName);
rg.addView(rb[current]);
/*other suff...