单击按钮btn2删除最后添加的行(由按钮btn1添加)

时间:2016-02-13 15:43:57

标签: android

在下面的代码中,按钮btn1正在添加一行(它正常工作)。单击按钮btn2后如何删除最后添加的行?

btn1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TableRow row1 = new TableRow(create_form.this);
        TextView txt1 = new TextView(create_form.this);
        EditText edttxt1 = new EditText(create_form.this);
        txt1.setText("Topic");
        TableRow row2 = new TableRow(create_form.this);
        TextView txt2 = new TextView(create_form.this);
        EditText edttxt2 = new EditText(create_form.this);
        txt2.setText("Employee Name");
        row1.addView(txt1);
        row1.addView(edttxt1);
        row2.addView(txt2);
        row2.addView(edttxt2);
        t1.addView(row1);
        t1.addView(row2);
    }
});

btn2.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View v) {
        // What code to add here to delete the last added row?
    }
});

2 个答案:

答案 0 :(得分:0)

你可以试试这个。此代码可能对您有所帮助。

  final TableRow tempTrRow1 = (TableRow)  tlRow.getChildAt(tblMain.getChildCount()-1);
        tblMain.removeView(tempTrRow1);

答案 1 :(得分:0)

TableRow lastRow = (TableRow)t1.getChildAt(t1.getChildCount() - 1);
t1.removeView(lastRow);