我想添加带有文本视图的动态布局,并在按钮上单击N级

时间:2017-11-27 06:52:25

标签: android user-interface android-edittext textview

image

点击添加手机短信时,我想动态添加视图(查看下方添加手机:其中'手机' textview ,右侧有编辑文本)

这需要为N级..当用户点击添加手机视图时,将添加..

如何做到这一点。?

2 个答案:

答案 0 :(得分:1)

试试这个代码。这可能会对你有所帮助。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

<Button
    android:id="@+id/btn_Click"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:text="Click" />

<LinearLayout
    android:id="@+id/linearView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" />
</LinearLayout>

在Activity类的on按钮上单击只使用下面的方法。并确保声明一个int变量(index),它将帮助你将新的View添加到最后。

 int index=0;
 linearView = (LinearLayout) findViewById(R.id.linearView);

@OnClick(R.id.btn_Click)
public void click() {
    LinearLayout mainLinearLayout = new LinearLayout(this);
    mainLinearLayout.setOrientation(LinearLayout.VERTICAL);

    LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    mainLinearLayout.setLayoutParams(mainParams);
    mainLinearLayout.setGravity(Gravity.CENTER);

    LinearLayout firstChildLinearLayout = new LinearLayout(this);
    firstChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);

    LinearLayout.LayoutParams firstChildParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    firstChildLinearLayout.setLayoutParams(firstChildParams);

    TextView textView = new TextView(this);
    LinearLayout.LayoutParams txtParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
    txtParams.setMarginStart(5);
    txtParams.setMarginEnd(10);
    textView.setGravity(Gravity.CENTER_VERTICAL);
    textView.setLayoutParams(txtParams);

    textView.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.ic_foreground), null, null, null);
    textView.setText("mobile  >");

    EditText editText = new EditText(this);
    LinearLayout.LayoutParams etParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
    etParams.weight = 1;
    editText.setBackground(null);
    editText.setLayoutParams(etParams);


    firstChildLinearLayout.addView(textView, 0);
    firstChildLinearLayout.addView(editText, 1);

    LinearLayout secondChildLinearLayout = new LinearLayout(this);
    secondChildLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
    secondChildLinearLayout.setBackgroundColor(getResources().getColor(R.color.color_grey));

    LinearLayout.LayoutParams secondChildParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
    secondChildLinearLayout.setLayoutParams(secondChildParams);


    mainLinearLayout.addView(firstChildLinearLayout, 0);
    mainLinearLayout.addView(secondChildLinearLayout, 1);

    linearView.addView(mainLinearLayout, index);

    index++;
}

因此,要从EditText获取值,请使用以下代码。在Toast中显示了值,您可以使用String数组来存储所有动态创建的EditText值。

    public void getAllEditTextValues(View view) {
    View v = null;
    for (int i = 0; i < linearView.getChildCount(); i++) {
        v = linearView.getChildAt(i);
        if (v instanceof LinearLayout) {
            View tempView = ((LinearLayout) v).getChildAt(0);
            View et = ((LinearLayout) tempView).getChildAt(1);
            String etValue = null;
            if (et instanceof EditText) {
                etValue = ((EditText) et).getText().toString();
            }
            Toast.makeText(this, "" + etValue, Toast.LENGTH_SHORT).show();
            // Use Array to Store all values of EditText
        }
    }
}

答案 1 :(得分:0)

将XML创建为&#34; custom_row.xml&#34;代码:

ul {
  white-space: nowrap;
}

现在将活动布局设为:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">
<ImageView
    android:layout_width="0dp"
    android:layout_weight="0.1"
    android:id="@+id/crossImage"
    android:layout_gravity="center_vertical"
    android:layout_height="wrap_content"
    android:src="@drawable/cross_image"/>

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.45"
    android:id="@+id/columnSpinner"
    android:text="demo"

    android:layout_gravity="center"/>

<EditText
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.45"
    android:id="@+id/contentValueEditText"/>

</LinearLayout>

现在代码为:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:id="@+id/linearView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
 <Button
android:id="@+id/btn_Click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Click" />


</LinearLayout>

现在您可以通过以下代码访问编辑文本:

  int index=0;
  linearView = (LinearLayout) findViewById(R.id.linearView);

 @OnClick(R.id.btn_Click)
 public void click() {
final LinearLayout singleInsertView1 = (LinearLayout) LayoutInflater.from(QueryRunner.this).inflate(R.layout.insert_single_item, null);

   EditText editText = (EditText)singleInsertView1.findViewById(R.id.contentValueEditText);
   editText.setTag("EditText"+index);

   singleInsertView1.setTag("LinearLayout"+index);
   contentViewAdder.addView(singleInsertView1);

   index++;
}