gpt4 book ai didi

java - Android:单击按钮后如何显示更多 View

转载 作者:行者123 更新时间:2023-11-30 00:53:07 25 4
gpt4 key购买 nike

解决了下面的代码

如果我有类似于下面的布局:

enter image description here

这五个临时 EditTexts 代表用户可以输入的一些信息(比如商品的价格、订单号等)如果用户想要添加另一个商品,他们可以点击添加按钮,我希望另外 5 个 TextView 出现在屏幕上这两个按钮的正上方,但恰好在前一组 5 个 EditTexts 的下方。有人可以给我一个关于我将如何做到这一点的起点。

我的 fragment 布局是这样的:

  • 我有一个顶级线性布局(垂直方向)。
  • 然后是 ScrollView 。
  • 在 ScrollView 中我有另一个线性布局。
  • 在那个线性布局中,我有五个 EditText 对象和两个按钮

上面的 View 是我在 MainActivity 文件中传递给 FragmentAdapter 的 fragment (在下面的文件中定义):

public class Device extends Fragment {
ScrollView scrollView;
LinearLayout ll;

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.device_view, container, false);

scrollView = (ScrollView) rootView.findViewById(R.id.device_scroll_view);
ll = (LinearLayout) rootView.findViewById(R.id.layout_in_scrollview);

Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);

addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
View temp = inflater.inflate(R.layout.edit_text_view_objects, container, false);
ll.addView(temp);
}
});
return rootView;
}
}

这是我的这个 fragment 的布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/device_fragment_linear_layout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/device_scroll_view">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="40dp">
<Button
android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Submit" />

<Button
android:id="@+id/add_another_device_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:layout_alignParentRight="true"/>
</RelativeLayout>
</LinearLayout>

edit_text_view_objects.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name of Master Device"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Device Name"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Max SMS per day"
android:gravity="center"
android:textSize="15dp"/>

<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:textSize="15dp"
android:hint="Carrier Name"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="OS Version"
android:layout_marginTop="10dp"/>

</LinearLayout>

最佳答案

在你的 XML 中取

<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/device_scroll_view">
<LinearLayout
android:id="@+id/layoutDynamic"a
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal">
</LinearLayout>
</ScrollView>

并为您的项目再创建一个 XML 并进行相应的设计(当您单击“添加”时它将动态添加)动态项目.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/device_fragment_linear_layout"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="DeviceFragment"
android:orientation="vertical">

<TextView
android:id="@+id/etDynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="15dp"
android:gravity="center"
android:layout_gravity="center"
android:hint="Color"
android:layout_marginTop="10dp"/>
</LinearLayout>

所以来到java代码

// take the reference of LinearLayout
linearLayout = (LinearLayout) romptView.findViewById(R.id.layoutDynamic);


// Take the reference of Add button

Button addButton = (Button) rootView.findViewById(R.id.add_another_device_button);

addButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final View addView = layoutInflater1.inflate(R.layout.dynamic_row, null);
final TextView textView1 = (TextView) addView.findViewById(R.id.etDynamic);
textView1.setText(otheret.getText().toString().trim()); otheret.setText(""); linearLayout.addView(addView);
}
});


#and if you want to remove particular item
removeIcon.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
addView.getParent()).removeView(addView);
}
});

关于java - Android:单击按钮后如何显示更多 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40604016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com