我打算使用这个程序,当您按下按钮向表格添加一行时,但它给了我错误,有人可以识别您正在做的任何错误吗?
这是主要的:
public class MainActivity extends Activity {
Button b1;
TableLayout tl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup the layout
setContentView(R.layout.activity_main);
// add a click-listener on the button
b1 = (Button) findViewById(R.id.b1);
tl = (TableLayout) findViewById(R.id.tLt);
TableRow tr_head = new TableRow(this);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView label_name = new TextView(this);
label_name.setText("Nome");
label_name.setTextColor(Color.WHITE);
tr_head.addView(label_name);
TextView label_surname = new TextView(this);
label_surname.setText("Apelido");
label_surname.setTextColor(Color.WHITE);
tr_head.addView(label_surname);
tl.addView(tr_head, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//botão
EditText name = (EditText) findViewById(R.id.eTname);
EditText subname = (EditText) findViewById(R.id.eTsubname);
String nname = name.getText().toString();
String sname = subname.getText().toString();
adicionalinha adicionar = new adicionalinha();
adicionar.adiciona(nname, sname, tl);
}
});
}
}
这是另一个功能 adicionalinha:
public class adicionalinha extends Activity {
public void adiciona(String name, String subname, TableLayout tl){
// Cria linha
TableRow tr = new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView labelNAME = new TextView(this);
labelNAME.setText(name);
labelNAME.setTextColor(Color.WHITE);
tr.addView(labelNAME);
TextView labelAPELIDO = new TextView(this);
labelAPELIDO.setText(subname);
labelAPELIDO.setTextColor(Color.WHITE);
tr.addView(labelAPELIDO);
// adiciona linha
tl.addView(tr);
}
}