【问题标题】:get TextView and EditText from Programatically created从 Programatally created 获取 TextView 和 EditText
【发布时间】:2016-11-16 19:34:03
【问题描述】:

我在 LinearLayout 中创建了 1 个 LinearLayout,其中包含 1 个 EditText 和 1 个 TextView,我需要从 EditText 和 TextView 中获取文本。这是我的代码:

main.xml:

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp" >

        <LinearLayout
            android:id="@+id/result"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:gravity="right"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

MainActivity.java:

    linearLayout = (LinearLayout) view.findViewById(R.id.result);

    .......

 public void addItem(){
    LinearLayout childLayout = new LinearLayout(context);
    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    childLayout.setLayoutParams(linearParams);
    childLayout.setTag(item_name);
    childLayout.setOrientation(LinearLayout.HORIZONTAL);


    TextView item = new TextView(context);
    item.setTag(item_name);
    item.setText(item_name);
    item.setTextSize(16);
    item.setGravity(Gravity.LEFT);
    LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    llp1.setMargins(0, 0, 15, 0);
    item.setLayoutParams(llp1);
    item.setTextColor(context.getResources().getColor(android.R.color.black));

    EditText quantity = new EditText(context);
    quantity.setText(nama_barang);
    quantity.setTextSize(16);
    quantity.setGravity(Gravity.CENTER_HORIZONTAL);
    LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    llp2.setMargins(10, 0, 15, 0);
    quantity.setLayoutParams(llp2);
    quantity.setTextColor(context.getResources().getColor(android.R.color.black));

    childLayout.addView(item);
    childLayout.addView(quantity);
    linearLayout.addView(childLayout);
 }

请帮帮我。

我已经编辑了我的问题

实际上,我上面的情况发生在我单击 addItem() 时,我的应用程序将显示新的 TextView 和 EditText,因此如果此过程完成,并移动到下一个 Fragment 以显示 TextViews 和 EditTexts 的所有文本,就像这样使用字符串生成器:

Items    Qtt
Cola......1

Chicken...1

Meat......2

帮帮我。

【问题讨论】:

    标签: android android-edittext textview programmatically-created


    【解决方案1】:

    你想变成这样吗?

    String str = item.getText().toString();
    String str2 = quantity.getText().toString();
    

    【讨论】:

    • 是的,但我的问题是,我想从 TextViews 中获取所有文本,并从 LinearLayout 中的所有 ChildLayout 中获取所有 EditTexts。 TextView & EditText 更多,而不仅仅是 for 条件创建的。
    • 好的,我们可以创建一个数组来获取所有的字符串。在末尾声明 for List&lt;String&gt; strings = new ArrayList&lt;&gt;(); 之外添加它们 strings.add(str);strings.add(str2);
    • 抱歉,我已经编辑了我的问题。请检查上面。谢谢
    【解决方案2】:

    如上所述,您只需要使用item.getText().toString();,但我认为您的问题在于您在循环内声明您的视图,稍后在代码的任何地方进行本地操作,您不会获取任何文本。

    尝试将您的视图声明为全局变量或在循环之外执行,例如;

    //....
    
    TextView item;
    EditText quantity;    
    
    
    for(int i = 0; i < cartCount; i++) {
    LinearLayout childLayout = new LinearLayout(context);
    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    childLayout.setLayoutParams(linearParams);
    childLayout.setTag(item_name);
    childLayout.setOrientation(LinearLayout.HORIZONTAL);
    
    
    item = new TextView(context);
    item.setTag(item_name);
    item.setText(item_name);
    item.setTextSize(16);
    item.setGravity(Gravity.LEFT);
    LinearLayout.LayoutParams llp1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    llp1.setMargins(0, 0, 15, 0);
    item.setLayoutParams(llp1);
    item.setTextColor(context.getResources().getColor(android.R.color.black));
    
    quantity = new EditText(context);
    quantity.setText(nama_barang);
    quantity.setTextSize(16);
    quantity.setGravity(Gravity.CENTER_HORIZONTAL);
    LinearLayout.LayoutParams llp2 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    llp2.setMargins(10, 0, 15, 0);
    quantity.setLayoutParams(llp2);
    quantity.setTextColor(context.getResources().getColor(android.R.color.black));
    
    childLayout.addView(item);
    childLayout.addView(quantity);
    linearLayout.addView(childLayout);
    

    }

    另一方面,通过查看您的代码,您是否尝试创建一个 EditText 和 TextViews 列表?如果是这样,明智的做法是考虑使用带有自定义项目的ListViewHere 你可以找到一个很好的教程来做类似的事情。

    【讨论】:

    • 其实,我的应用程序。显示“添加项目”按钮。因此,如果单击“添加项目”,则会显示以编程方式创建的 TextView 和 EditText。我的问题是,如果我移动到下一个片段,我想显示每个 TextViews 和 EditTexts ..
    • ...使用 StringBuilder。
    【解决方案3】:

    这感觉就像您应该查看适配器视图,例如回收站视图。使用线性布局时,您可能会开始看到一些性能问题。

    无论如何。您可以在将视图添加到线性布局时将它们添加到列表中。

    List<TextView> items = new LinkedList<>();
    List<EditText> quantities = new LinkedList<>();  
    for(int i = 0; i < cartCount; i++) {
       ...
        childLayout.addView(item);
        childLayout.addView(quantity);
        items.add(item);
        quantities.add(quantity);
        linearLayout.addView(childLayout);
    }
    

    然后循环浏览您的视图

    for (TextView textView : items) {
    
    }
    
    for (EditText editText : quantities) {
    
    }
    

    【讨论】:

    • 对不起,我已经编辑了我上面的问题,请检查,谢谢
    【解决方案4】:
    item.getText().toString()
    quantity.getText().toString()
    

    【讨论】:

    • 请阅读我上面的源代码,兄弟。我需要从 LinearLayout 的所有 childLayout 中获取所有 TextViews 文本和所有 EditTexts 文本。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多