2017-10-11 59 views
1

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

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

眼下,单选按钮显示我的手机屏幕上的罚款(以XML为tablelayout设置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> 

这基本上就是我试图让我的tablerows看起来像: https://image.ibb.co/i5qrtw/goodtablerowpmr.png

+0

https://stackoverflow.com/questions/11891105/have-horizo​​ntal-radiobuttons-wrap-if-too-long屏幕 – HomeIsWhereThePcIs

+0

该帖子的两个回复均未回答提出的问题;第一个不适用于radiogroups,第二个只会在额外的按钮不足的时候将其移除。我希望我的行是统一的,并且在按钮外观上都有2个按钮。 – devander

+0

第一个答案使用扩展RadioGroup的引用的MultiLineRadioGroup类。 – HomeIsWhereThePcIs

回答

0

的MultiLineRadioGroup类建议只是没有以多种方式工作,所以我回到了绘图板。我找到的解决方案就是完全跳出电台小组,我放置了一个包含三个单选按钮的RelativeLayout,并使用LayoutParams以我想要的方式放置按钮并设置它们的宽度/高度/重量,而不是表格行中的radiogroup。然后,我为每个单选按钮创建单独的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"); 
     } 
    }); 
} 
相关问题