注解:
- 直接显示
TableLyout
会出现显示不完全的情况,以后有时间再研究为什么,目前的解决方案是外侧套一层LinearLayout
。 - 写的这么散是为了便于与XML布局做对比,以控件为单位。
TableHelp_PZ类
public class TableHelp_PZ {
Context context;
public TableHelp_PZ(Context context) {
this.context = context;
}
public LinearLayout getRadioTable(String[] list){
return new MyLinearLayout(list);
}
public class MyLinearLayout extends android.widget.LinearLayout{
public MyLinearLayout(String[] list) {
super(context);
setGravity(Gravity.CENTER);
setOrientation(VERTICAL);
addView(new MyTableLayout(list));
}
}
public class MyTableLayout extends TableLayout {
public MyTableLayout(String[] list) {
super(context);
setGravity(Gravity.CENTER);
setBackgroundColor(getResources().getColor(R.color.black));//直接R.color无效
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20,0,20,0);
setLayoutParams(layoutParams);
for (int i=0;i<list.length;i++){
addView(new MyTableRow("\t"+list[i]+"\t",i));
}
}
}
public class MyTableRow extends TableRow {
public MyTableRow(String string,int i) {
super(context);
setGravity(Gravity.CENTER);
setBackgroundColor(getResources().getColor(R.color.white));//直接R.color无效
TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
if (i==0){//为第一行构建上框线
layoutParams.setMargins(1,1,1,1);
}else {
layoutParams.setMargins(1,0,1,1);
}
setLayoutParams(layoutParams);
addView(new MyTextView(string));
addView(new MyTextView(R.color.black));
addView(new MyRadioGroup());
}
}
public class MyTextView extends android.support.v7.widget.AppCompatTextView{
public MyTextView(String string) {
super(context);
setText(string);
setTextColor(getResources().getColor(R.color.black));//直接R.color无效
setGravity(Gravity.CENTER);
}
public MyTextView(int color) {
super(context);
setWidth(1);
setBackgroundColor(getResources().getColor(color));
MyTableRow.LayoutParams layoutParams = new MyTableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
setLayoutParams(layoutParams);
}
}
public class MyRadioGroup extends RadioGroup {
public MyRadioGroup() {
super(context);
setOrientation(RadioGroup.HORIZONTAL);
addView(new MyRadioButton("正常"));
addView(new MyRadioButton("修好"));
addView(new MyRadioButton("有问题但尚能生产"));
addView(new MyRadioButton("发生故障不能使用"));
addView(new MyRadioButton("该日未使用本设备"));
}
}
public class MyRadioButton extends android.support.v7.widget.AppCompatRadioButton {
public MyRadioButton(String string) {
super(context);
setText(string);
RadioGroup.LayoutParams layoutParams = new RadioGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(5,0,5,0);
setLayoutParams(layoutParams);
}
}
}
主函数MainActivity调用
public class MainActivity extends Activity implements DialogInterface.OnDismissListener {
LinearLayout linearLayout;
String[] messsage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messsage = new String[]{"检查各按钮是否有损坏","dfdsafadsg","刀库切削是否清理干净"};
linearLayout = new TableHelp_PZ(this).getRadioTable(messsage);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("检点表");
builder.setCancelable(false);
builder.setView(linearLayout);
builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setOnDismissListener(this);
builder.create().show();
}
@Override
public void onDismiss(DialogInterface dialog) { ((FrameLayout)linearLayout.getParent()).removeAllViews(); }
}
需要获取控件的情况下,通过getChildAt()
与getChildCount()
对布局进行筛选。
我这里提供参考,获取RadioButton
的选择项Text:
public class MainActivity extends Activity implements DialogInterface.OnDismissListener {
String TAG ="MainActivity";
LinearLayout linearLayout;
String[] messsage;
RadioGroup[] radioGroups;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
messsage = new String[]{"检查各按钮是否有损坏","dfdsafadsg","刀库切削是否清理干净"};
linearLayout = new TableHelp_PZ(this).getRadioTable(messsage);
//获取RadioGroup的个数,也就是行数
TableLayout tableLayout = (TableLayout)linearLayout.getChildAt(0);
int rowcount = tableLayout.getChildCount();
//构建对象数据集
radioGroups = new RadioGroup[rowcount];
for (int i=0;i<rowcount;i++){
radioGroups[i] = (RadioGroup) ((TableRow)tableLayout.getChildAt(i)).getChildAt(2);
}
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("检点表");
builder.setCancelable(false);
builder.setView(linearLayout);
builder.setPositiveButton("提交", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//处理提交,这里我提供一个获取选择项Text的方法
Log.d(TAG, "ButtonText: "+getCheckdText(radioGroups[0].getCheckedRadioButtonId()));
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setOnDismissListener(this);
builder.create().show();
}
//未勾选则返回空字符串
private String getCheckdText(int id) {
if(id==-1) {
return "";
}else {
return ((RadioButton)linearLayout.findViewById(id)).getText().toString();
}
}
@Override
public void onDismiss(DialogInterface dialog) { ((FrameLayout)linearLayout.getParent()).removeAllViews(); }
}