1

我正在创建一个添加联系人活动。只有一个编辑文本和一个按钮 (+),当单击按钮时,应添加另一个编辑文本,前一个按钮应变为 (-)。通过这种方法,我想添加和删除编辑文本字段。像这样的东西。我正在这样做,但无法实现我想要的。任何的想法?

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);
        }

    }
}
4

3 回答 3

3

在您的活动中使用此代码:

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    // TODO Auto-generated method stub

     if (event.getKeyCode() == KeyEvent.KEYCODE_PLUS)
     {
            if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0)
            {
                LinearLayout mLinearLayout = new LinearLayout(this);
                mLinearLayout = (LinearLayout)findViewById(R.id.mylinearlayout);

                                EditText lEditText = new EditText(this);
                            lEditText.SetText("Text Here");
                            mLinearLayout.addView(lEditText);

                    }
            }
    return super.dispatchKeyEvent(event);
}
于 2013-02-18T12:08:09.013 回答
2
private void addEditView() {
        // TODO Auto-generated method stub
          LinearLayout li=new LinearLayout(this);
          EditText et=new EditText(this);
          Button b=new Button(this);

          b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                int pos=(Integer) v.getTag();
                mainLayout.removeViewAt(pos);

            }
        });

          b.setTag((mainLayout.getChildCount()+1));
    }

当您单击 addEditText 按钮时调用此函数。

于 2013-02-18T12:24:46.390 回答
0

您应该创建 Android 布局(可能是 LinearLayout 或 TableLayout),然后在 java 代码中创建视图(Android Activity)

简单的例子;

视图视图= 新视图(此);

如果视图创建成功,您可以添加此代码 --> .addView( view ); 或者如果已添加视图,您可以删除此代码示例 --> .removeView( view );

于 2013-02-18T12:20:25.443 回答