實現生成一些button,數量取決於用戶(用戶點擊按鈕,就會生成一個)
具體代碼:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layoutcprompt);
RelativeLayout.LayoutParams OBJ = new RelativeLayout.LayoutParams (140,80);
if ((commandsi%6)==0) {adjust=adjust+86; commandsi=1;}
OBJ.leftMargin =(140*(commandsi-1))+10;
OBJ.topMargin =250+adjust;
Button command = new Button(this);
command.setLayoutParams(OBJ);
command.setId(ID);
command.setText(edittxt.getText().toString());
edittxt.setText("");
command.setBackgroundResource(R.drawable.costum_button);
command.setTextColor(Color.WHITE);
command.setTextSize(14);
layout.addView(command);
command.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Button b = (Button)view;
scommand=b.getText().toString();
}
});
command.setVisibility(View.VISIBLE);
現在還要實現移除這些button,我給每個都有單獨的id,但是不知道怎麼移除:/。
謝謝。
ViewGroup layout = (ViewGroup) command.getParent();
if(null!=layout) //for safety only as you are doing onClick
layout.removeView(command);
試試這個。