【问题标题】:android: radio buttons inside radio buttonsandroid:单选按钮内的单选按钮
【发布时间】:2014-05-30 08:09:42
【问题描述】:

我对 Android 很陌生,这是我的第一个应用程序,所以如果我的问题不清楚,请更正,我很乐意添加更多关于我想要实现的目标的信息。 我有一个动态构建的广播组。在这个单选组内,我希望有另一个单选组,这取决于从第一组中选择的单选按钮。 所以,假设我有一个项目数组列表,并且每个项目都有一些可用的尺寸(即:XS、S、L)。如果我选中单选按钮“XS”,我希望有另一个单选组,其中包含所选尺寸 XS 的可用颜色。 我构建它的方法是创建一个单选组,它是动态的单选按钮。在 onCheckedChanged() 方法中,我调用了 createRadioButtonsForAvailableColors() 方法。这个创建了具有选中尺寸所需颜色的单选按钮,但是一旦我在上部单选组中检查了另一个尺寸,该尺寸可用的新颜色将添加到之前选择的尺寸显示的颜色中。 谢谢你。 这是我的创建方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_product_details);

    sizesList = getSizes(getIDFromPreviousActivity());
    createRadioButtonsForAvailableSizes(sizesList);     
}

这是我为包含可用尺寸的单选按钮的单选组创建的:

// creates the radio buttons with the available sizes   
public void createRadioButtonsForAvailableSizes(ArrayList<String>  sizeList) {
    productDetailsLayout = (LinearLayout) findViewById(R.id.productDetailsLayout);
    RadioGroup rg = new RadioGroup(this);
    rg.setOrientation(RadioGroup.HORIZONTAL);


    int n = sizeList.size();
    final RadioButton[] rb = new RadioButton[n];
    for(int i=0; i< sizeList.size(); i++) {
        colorList = getColors(getIDFromPreviousActivity(),sizeList.get(i));

         rg.setOrientation(RadioGroup.HORIZONTAL);
         rb[i] = new RadioButton(this);
         rg.addView(rb[i]);
         rb[i].setText(sizesList.get(i).toString());
         rb[i].setId(getIDForRadioButton(sizesList.get(i).toString()));
         rb[i].setButtonDrawable(R.drawable.radiobuttonunchecked);
         rb[i].setOnCheckedChangeListener(this);

    }
    productDetailsLayout.addView(rg);
    productDetailsLayout.setPadding(50, 50, 50, 50);
}

这是颜色的创建(与尺寸相同):

// create radio buttons for available colors
public void createRadioButtonsForAvailableColors(ArrayList<String>  colorList) {

    Log.d("createRadioButtonsForAvailableColors","");
    productDetailsLayout = (LinearLayout) findViewById(R.id.productDetailsLayout);
    RadioGroup rg = new RadioGroup(this);
    rg.setOrientation(RadioGroup.HORIZONTAL);


    int n = colorList.size();
    final RadioButton[] rb = new RadioButton[n];
    for(int i=0; i< colorList.size(); i++) {
        Log.d("color"+i,colorList.get(i));
         rg.setOrientation(RadioGroup.HORIZONTAL);
         rb[i] = new RadioButton(this);
         rg.addView(rb[i]);
         rb[i].setText(colorList.get(i).toString());
         rb[i].setId(getIDForRadioButton(colorList.get(i).toString()));
         rb[i].setButtonDrawable(R.drawable.radiobuttonunchecked);

         rb[i].setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override public void onCheckedChanged(CompoundButton button, boolean isChecked) {
                    button.setButtonDrawable(isChecked ? R.drawable.radiobuttonchecked : R.drawable.radiobuttonunchecked);
                }
         });

    }
    productDetailsLayout.addView(rg);
    productDetailsLayout.setPadding(50, 50, 50, 50);
}

这是我的 onCheckedChange 方法:

     @Override 
 public void onCheckedChanged(CompoundButton button, boolean isChecked) {
        button.setButtonDrawable(isChecked ? R.drawable.radiobuttonchecked : R.drawable.radiobuttonunchecked);
        String text = button.getText().toString();
        int productID = getIDFromPreviousActivity();
        ArrayList<String> colorList = getColors(productID, text);
        createRadioButtonsForAvailableColors(colorList);
    }

内部组是正确创建的,但是在检查第一组中的一个大小时,它会正常构建子组单选按钮。如果我单击一种尺寸,它会显示可用的颜色。但是,在更改选中的尺寸时,显示当前所选尺寸颜色的按钮将添加到之前所选尺寸的按钮中。如何取消第一次选择尺寸时创建的内部按钮并仅显示当前所选尺寸的可用颜色?

我认为这可能不是构建它的正确方法。所以,我有两个问题:

  1. 通常,最好的方法是什么?由于我的内部单选组取决于从第一组中选择的单选按钮,我假设应该在 onCheckedChange() 方法中调用内部组的创建。我应该从这里开始另一项活动,还是可以在一项活动中完成所有活动?
  2. 如果我的方法是正确的,请告诉我如何删除创建的内部单选按钮,以防主组中的单选按钮被更改?

谢谢

【问题讨论】:

    标签: android android-radiogroup


    【解决方案1】:

    所以,如果我理解这一点,问题就出现在您更改尺寸单选按钮的值时,这意味着颜色不会消失。

    1. 嗯,Java 有很多样板,没有什么好说的(我可能会采用类似的方式)。

    2. 您可以在开始添加内容之前在 createRadioButtonsForAvailableColors 方法中添加它:

    productDetailsLayout.removeViews(0, productDetailsLayout.getChildCount());

    RemoveViews 将从某个位置开始删除任意数量的子视图(因此我将其传递为 0 和子视图总数)。

    【讨论】:

    • 感谢您的回答:是的,如果我选择比如说“XS”,它会显示 XS 尺寸的可用颜色。如果然后我选择“S”尺寸,它会在 XS 可用颜色下方添加 S 可用颜色。我从代码中知道可用颜色列表是根据大小正确构建的,这只是一个显示问题。是的,这可能有助于删除创建的第一个孩子。让我看看它是否有效。如果我必须这样做的话,调用另一个活动会更好吗?
    • 如果通过调用另一个活动来执行此操作,您的意思是在另一个活动中加载子单选按钮,那么这取决于对您的应用程序更有意义的内容。我个人会选择DialogFragment,因为您甚至可以仅使用单选按钮列表生成一个,而无需创建/删除视图的麻烦。因此,当您单击尺寸时,会弹出一个对话框片段,其中包含可用颜色列表。当您选择一个时,对话框将被关闭,并且所选值由 DialogFragment 事件之一返回给您。
    • 在此处查看更多信息:developer.android.com/reference/android/app/DialogFragment.html 但是,嘿,再次,这从根本上改变了您的问题,所以不要盲目地进行更改,并花一点时间来选择最适合您的界面的内容.
    • 我在那里试过,它也删除了上面的尺寸列表。所以它必须移动到其他地方,以便它只删除颜色
    • 对了,我忘了说。但是,您实际上可以更改 0 以适应您拥有的大小数量。因此,如果您有 4 种尺寸,请将其更改为 productDetailsLayout.removeViews(4, productDetailsLayout.getChildCount()-4);
    猜你喜欢
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-08
    相关资源
    最近更新 更多