【发布时间】: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