我想通过点击按钮以编程方式向RelativeLayout添加一对EditTexts

时间:2014-11-21 20:20:50

标签: android android-edittext textview relativelayout

第一组edittexts在另一个下面整齐地添加,但是再次单击该按钮,视图将重叠。我想我需要正确设置保证金,但我无法弄清楚。请帮忙。

public class MainActivity extends Activity implements OnClickListener {
    Button btn_task, btn_subTask;
    EditText et_task, et_subTask;
    RelativeLayout lv_subtask, lv_task;
    int i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_task = (Button) findViewById(R.id.btn_addTask);
        btn_subTask = (Button) findViewById(R.id.btn_subTask);
        lv_subtask = (RelativeLayout) findViewById(R.id.lv_subtask);
        lv_task = (RelativeLayout) findViewById(R.id.lv_task);

        // btn_task.setOnClickListener(this);
        btn_subTask.setOnClickListener(this);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {

        if (v.equals(btn_subTask)) {
            EditText et_title = new EditText(this);
            EditText et_date = new EditText(this);
            i++;
            et_title.setId(i);
            et_date.setId(i + 1);
            et_title.setHint("Enter sub task");
            et_date.setHint("Enter end date");
            // lv_subtask.setVisibility(View.VISIBLE);
            RelativeLayout.LayoutParams EditLayoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams DateLayoutParams = new RelativeLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            DateLayoutParams.setMargins(5, 10, 0, 0);
            EditLayoutParams.setMargins(5, 20, 0, 0);
            if (i != 1) {
                EditLayoutParams.addRule(RelativeLayout.BELOW, i + 1);
                DateLayoutParams.addRule(RelativeLayout.BELOW, i + 2);

            } else {
                DateLayoutParams.addRule(RelativeLayout.BELOW, 1);
            }
            et_title.setLayoutParams(EditLayoutParams);
            et_date.setLayoutParams(DateLayoutParams);
            (lv_subtask).addView(et_title);
            (lv_subtask).addView(et_date);

        }

    }
}

2 个答案:

答案 0 :(得分:0)

看起来你没有正确使用你的计数器。首先,每次调用onClick时,你只将它递增1,但是你要添加2个视图。此外,当您尝试将新视图置于之前的视图之下时,您添加到您的计数器,这将引用您尚未设置的ID。试试这个:

if (v.equals(btn_subTask)) {
    EditText et_title = new EditText(this);
    EditText et_date = new EditText(this);
    et_title.setId(++i); // 1, 3, ...
    et_date.setId(++i); // 2, 4, ...
    et_title.setHint("Enter sub task");
    et_date.setHint("Enter end date");
    // lv_subtask.setVisibility(View.VISIBLE);
    RelativeLayout.LayoutParams EditLayoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    RelativeLayout.LayoutParams DateLayoutParams = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    DateLayoutParams.setMargins(5, 10, 0, 0);
    EditLayoutParams.setMargins(5, 20, 0, 0);
    if (i != 2) {
        EditLayoutParams.addRule(RelativeLayout.BELOW, i - 2); // below last view added
    }
    DateLayoutParams.addRule(RelativeLayout.BELOW, i - 1); // always below current edit
    et_title.setLayoutParams(EditLayoutParams);
    et_date.setLayoutParams(DateLayoutParams);
    (lv_subtask).addView(et_title);
    (lv_subtask).addView(et_date);

}

答案 1 :(得分:0)

对于两个神奇地出现的Edittexts,在布局中隐藏了EditTexts,然后在按钮的OnClick中显示它们。诀窍在于,当您设计这些时,它们始终可见,并在应用程序启动时隐藏它们。

为可见性定义一个布尔值,然后在onResume中使用布尔来设置可见性。

  boolean ismyedittextvisible = false;


 public void onResume();
     super.onResume();
     view.findViewById(R.id.myedittext)
                .setVisibility(ismyedittextvisible ? View.VISIBLE : View.GONE);

在你的按钮onClick()toogle布尔值并刷新可见性

@Override
public void onClick(final View v) {
    if (v.getId() == R.id.mybutton) {
        ismyedittextvisisble = !ismyedittextvisible;
        view.findViewById(R.id.myedittext)
                .setVisibility(ismyedittextvisible ? View.VISIBLE : View.GONE);
    }
}