1

我一直试图只选中一个单选按钮,但每次用户都可以选择多个单选按钮。

如何只选中一个或当前选定的单选按钮?

我基本上有一个TableLayoutTableRow代码中膨胀的 XML 布局。此外,onClickListener在 RadioButton 上应用了an ,它tag为当前选定的单选按钮获取 。

有人可以帮我解决这个问题吗?

对于参考。我的表格行的 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);


        }
    });

}
4

1 回答 1

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...
于 2013-03-04T12:46:19.217 回答