android edittext添加按钮,Android在(+)按钮上添加edittext字段并通过点击删除( - )...

我正在创建添加联系人活动。只有一个编辑文本和一个按钮(+),当点击按钮时,应该添加另一个编辑文本,并且上一个按钮应该变成( - )。通过这种方法,我想添加和删除编辑文本字段。 Some thing like this.我这样做,但无法实现我想要的。任何想法?Android在(+)按钮上添加edittext字段并通过点击删除( - )

public class TextField extends Activity {

Context con;

TableLayout table;

TableRow tr[] = new TableRow[6];

EditText txt[] = new EditText[6];

//ImageView img[] = new ImageView[5];

Button img[]=new Button[6];

int count ;

int lastDeletedIndex;

int lastPopulatedIndex;

boolean isDel;

public void onCreate(Bundle obj) {

super.onCreate(obj);

// Log.d("","On Create");

con = this;

count = 1;

table = new TableLayout(this);

tr[count] = new TableRow(this);

tr[count].setId(count);

tr[count].setLayoutParams(new TableRow.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

txt[count] = new EditText(this);

txt[count].setId(count);

txt[count].setText("1");

txt[count].setMaxLines(1);

txt[count].setWidth(160);

img[count] = new Button(this);

img[count].setId(count);

img[count].setText("+");

img[count].setBackgroundResource(R.drawable.add);

img[count].setPadding(5, 7, 20, 0);

tr[count].addView(txt[count]);

tr[count].addView(img[count]);

tr[count].setPadding(20, 20, 0, 0);

img[count].setOnClickListener(new OnClick(img[count]));

table.addView(tr[count]);

setContentView(table);

}

class OnClick implements OnClickListener {

View view;

Button addIcon;

public OnClick(View view) {

this.addIcon = (Button) view;

}

@Override

public void onClick(View arg0) {

int id=addIcon.getId();

if(addIcon.getText().toString().equals("-"))

{

if ((id >1 )) {

isDel=true;

count=id;

lastDeletedIndex=id;

txt[count]=null;

img[count]=null;

table.removeView(tr[count]);

tr[count]=null;

Log.d("", "Cancel img Id" + addIcon.getId());

}

} else {

if (count <5) {

if(table.getChildCount()!=5)

{

addIcon.setBackgroundDrawable(getResources().getDrawable(R.drawable.delete));

addIcon.setText("-");

addIcon.setId(count);

if(!isDel) {

count++;

}else

{

count=lastDeletedIndex;

}

tr[count] = new TableRow(con);

tr[count].setId(count);

img[count] = new Button(con);

img[count].setId(count);

img[count].setBackgroundDrawable(getResources().getDrawable(R.drawable.add));

img[count].setText("+");

img[count].setPadding(3, 7, 20, 0);

img[count].setOnClickListener(new OnClick(img[count]));

txt[count] = new EditText(con);

txt[count].setId(count);

txt[count].setText(""+count);

txt[count].setMaxLines(1);

txt[count].setWidth(160);

tr[count].addView(txt[count]);

tr[count].addView(img[count]);

tr[count].setPadding(20, 20, 0, 0);

table.addView(tr[count]);

setContentView(table);

isDel=false;

lastPopulatedIndex=count;

Log.d("", "Cancel img Id" + addIcon.getId());

}

} else

ShowDialog.showAlert("Info","You can't add more than 5 contacts!", con);

}

}

}

2013-02-18

Sandeep

+0

你见过SDK样本那里你可以找到这个 –

2013-02-18 11:58:14

+0

的很好的例子是,你需要开始阅读“的Android版本:动态创建傻瓜布局”。如果你有更具体的问题,请回来,在这里问问! –

2013-02-18 11:59:35

+0

你有没有使用edittext的子视图,将很容易添加和删除按钮单击视图.. –

2013-02-18 11:59:45

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值