【问题标题】:Moving one radiobutton of a 3 button radiogroup to a second line in its Tablerow将 3 按钮单选组的一个单选按钮移动到其表格行中的第二行
【发布时间】:2018-03-23 16:51:11
【问题描述】:

我正在做一个库存管理 Android 应用程序,我在其中以编程方式动态创建行以放入我的 xml 定义的表格布局中。对于每个项目,我创建一个包含 2 个视图的表行:一个包含库存项目名称的文本视图和一个包含 3 个单选按钮的单选按钮:存在、缺失和需要维修。在 tablelayout 的每一行下面还有一个 linearlayout,其中包含一个 textview 和一个 edittext,但该部分没有任何问题。

这是我的屏幕截图,显示了它目前的样子: https://preview.ibb.co/k8a7Yw/Screenshot_20171011_170618.png

现在,单选按钮在我的手机屏幕上显示正常(在为 tablelayout 设置 xml 中的 shrinkcolumn=0 后,第三个单选按钮的文本不会被截断),但项目名称被压扁。我想要发生的是将最后一个单选按钮(需要修复)向下移动一行,但将其保留在 radiogroup 和 row 中,这样我就不必创建从 radiogroup 继承的自定义类来让它正确切换.

当前代码:

public class PMR {
    String result;
    String caption;
    String repairString;
    EditText repairEditText;
    RadioButton pRadioButton;
    RadioButton mRadioButton;
    RadioButton rRadioButton;

    public PMR(){
    }
    public PMR(final TableLayout tableLayout, final TableRow tableRow, 
    final Context context, String caption){
        this.caption=caption;
        result="incomplete";
        repairString=null;
        final RadioGroup radioGroup=new RadioGroup(context);
        radioGroup.setTag("incomplete");
        radioGroup.setOrientation(RadioGroup.HORIZONTAL);
        tableRow.addView(radioGroup);

        pRadioButton = new RadioButton(context);
        pRadioButton.setText("Present");
        pRadioButton.setId(generateViewId());
        final int pId=pRadioButton.getId();

        mRadioButton = new RadioButton(context);
        mRadioButton.setText("Missing");
        mRadioButton.setId(generateViewId());
        final int mId=mRadioButton.getId();

        rRadioButton = new RadioButton(context);
        rRadioButton.setText("Repairs Needed");
        rRadioButton.setId(generateViewId());
        final int rId=rRadioButton.getId();

        radioGroup.addView(pRadioButton);
        radioGroup.addView(mRadioButton);
        radioGroup.addView(rRadioButton);

        final LinearLayout textLayout=new LinearLayout(context);
        textLayout.setOrientation(LinearLayout.HORIZONTAL);
        tableLayout.addView(textLayout);
        textLayout.setTag("Text Row");

        final TextView labelText = new TextView(context);
        labelText.setText("");
        textLayout.addView(labelText);

        repairEditText = new EditText(context);
        repairEditText.setEnabled(false);
        repairEditText.setText("");
        textLayout.addView(repairEditText);

        final String prefix="Notes: ";

        radioGroup.setOnCheckedChangeListener(new 
        RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged (RadioGroup group, 
            int checkedId){

                if (checkedId!=rId && 
                repairEditText.getText().toString()!=null){
                   repairString=repairEditText.getText().toString();
                }
                if (checkedId == pId) {
                    result="Present";
                    repairEditText.setText("");
                    repairEditText.setEnabled(false);
                    labelText.setText("");
                    radioGroup.setTag("pTag");
                } else if (checkedId == mId) {
                    result="Missing";
                    repairEditText.setText("");
                    repairEditText.setEnabled(false);
                    labelText.setText("");
                    radioGroup.setTag("mTag");
                } else if (checkedId == rId) {
                    result="Repairs Needed";
                    if (repairString!=null){
                        repairEditText.setText(repairString);
                    }
                    repairEditText.setEnabled(true);
                    repairEditText.requestFocus();
                    labelText.setText(prefix);
                    radioGroup.setTag("rTag");
                }
                else {
                    result = "incomplete";
                    radioGroup.setTag("incomplete");
                }
            }
        });
    }
}

使用tablelayout xml:

<TableLayout
                android:id="@+id/tableLayout2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:divider="?android:attr/dividerHorizontal"
                android:showDividers="middle"
                android:shrinkColumns="0"
                >
</TableLayout>

这基本上就是我想让我的表格看起来像的样子: https://image.ibb.co/i5qrtw/goodtablerowpmr.png

【问题讨论】:

  • 该帖子的两个回复都没有回答提出的问题;第一个不适用于无线电组,第二个仅在空间不足时将附加按钮向下移动。我希望我的行是统一的,并且在 1 个按钮的外观上都有 2 个按钮。
  • 第一个答案使用引用的 MultiLineRadioGroup 类,它扩展了 RadioGroup。
  • 他们改变了顺序。 multilineradiogroup 类仅在空间不足时才进行包装。无论左侧的文本有多长,我都希望它设置按钮为 2 比 1 的位置。
  • 我猜你可以通过改变 MultiLineRadioGroup 类中的布局逻辑来达到同样的效果。 if(nextBottom > maxBottom){ // 如果当前按钮将超出此列的边框...您可以使用以下内容进行更改: if (x>2 && x%2==1)

标签: android layout radio-button tablerow radio-group


【解决方案1】:

建议的 MultiLineRadioGroup 类不能以多种方式工作,所以我回到了绘图板上。我找到的解决方案是完全跳过广播组;我在表格行中放置了一个包含三个单选按钮的 RelativeLayout,而不是单选组,并使用 LayoutParams 以我想要的方式放置按钮并设置它们的宽度/高度/重量。然后,我为每个单选按钮创建了单独的 onClickListener,而不是 onCheckedChangeListener。因此,虽然我最终不得不编写自己的类和函数,但事实证明这是值得的。 我的自定义类:

public class FancyRadioGroup{
    public FancyRadioGroup(Context context, TableRow tableRow, TextView 
textView, RadioButton pButton, RadioButton mButton, RadioButton rButton){
        TableRow.LayoutParams rowParams= new 
TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 
TableRow.LayoutParams.MATCH_PARENT, 1.0f);
        textView.setLayoutParams(rowParams);

        RelativeLayout relativeLayout = new RelativeLayout(context);
        tableRow.addView(relativeLayout);
        relativeLayout.setLayoutParams(rowParams);

        relativeLayout.addView(pButton);
        relativeLayout.addView(mButton);

        RelativeLayout.LayoutParams topLParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT);
        topLParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        pButton.setLayoutParams(topLParams);

        RelativeLayout.LayoutParams topRParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT);
        topRParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        topRParams.addRule(RelativeLayout.RIGHT_OF,pButton.getId());
        mButton.setLayoutParams(topRParams);

        RelativeLayout.LayoutParams botParams= new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT);
        botParams.addRule(RelativeLayout.BELOW,mButton.getId());
        relativeLayout.addView(rButton);
        rButton.setLayoutParams(botParams);
    }
}

然后是编辑后的现有类:

public PMR(final TableLayout tableLayout, final TableRow tableRow, final 
TextView textView, final Context context, String caption){
    this.caption=caption;
    textView.setTag("incomplete");
    repairString=null;

    pRadioButton = new RadioButton(context);
    pRadioButton.setText("Present");
    pRadioButton.setId(generateViewId());
    final int pId=pRadioButton.getId();

    mRadioButton = new RadioButton(context);
    mRadioButton.setText("Missing");
    mRadioButton.setId(generateViewId());
    final int mId=mRadioButton.getId();

    rRadioButton = new RadioButton(context);
    rRadioButton.setText("Repairs Needed");
    rRadioButton.setId(generateViewId());
    final int rId=rRadioButton.getId();

    final FancyRadioGroup fancyRadioGroup = new FancyRadioGroup(context, 
tableRow, textView, pRadioButton, mRadioButton, rRadioButton);

    final LinearLayout textLayout=new LinearLayout(context);
    textLayout.setOrientation(LinearLayout.HORIZONTAL);
    tableLayout.addView(textLayout);
    textLayout.setTag("Text Row");

    final TextView labelText = new TextView(context);
    labelText.setText("");
    textLayout.addView(labelText);

    repairEditText = new EditText(context);
    repairEditText.setEnabled(false);
    repairEditText.setText("");
    textLayout.addView(repairEditText);

    final String prefix="Notes: ";
    pRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRadioButton.setChecked(false);
            if (repairEditText.isEnabled()){
                repairEditText.setEnabled(false);
                labelText.setText("");
            }
            textView.setTag("Present");
            rRadioButton.setChecked(false);
        }
    });
    mRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            pRadioButton.setChecked(false);
            if (repairEditText.isEnabled()){
                repairEditText.setEnabled(false);
                labelText.setText("");
            }
            textView.setTag("Missing");
            rRadioButton.setChecked(false);
        }
    });
    rRadioButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mRadioButton.setChecked(false);
            pRadioButton.setChecked(false);
            repairEditText.setEnabled(true);
            repairEditText.requestFocus();
            labelText.setText(prefix);
            textView.setTag("Repairs Needed");
        }
    });
}

【讨论】:

    【解决方案2】:

    RadioGroup 内部,创建一个LinearLayout 并放置第一行单选按钮。如果下一行只需要 1 个单选按钮,只需将 RadioButton 放置为 RadioGroup 的直接子代即可。像这样,

    <RadioGroup>
        <LinearLayout>
            ... 
        </LinearLayout>
    
        <RadioButton>
        </RadioButton>
    </RadioGroup>
    

    更新: 上述解决方案无效。 解决方案here 工作正常

    【讨论】:

      猜你喜欢
      • 2013-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-29
      • 2019-04-05
      • 2015-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多