【问题标题】:Android: How to display more views after clicking a buttonAndroid:单击按钮后如何显示更多视图
【发布时间】:2017-03-28 23:48:09
【问题描述】:

解决了下面的代码

如果我的布局类似于以下布局:

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

我的片段布局是这样的:

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

上面的视图是一个片段(在下面的文件中定义),我在我的 MainActivity 文件中传递给我的 FragmentAdapter:

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;
    }
}

这是我的这个片段的布局文件:

<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>

【问题讨论】:

  • 当我动态添加新视图时,我会制作我想要添加的模板,然后使用LayoutInflater添加它
  • 哦,好吧,您是说使用LayoutInflater 将其添加到scrollview 中?
  • @1290 试试这个把那些按钮放在滚动视图之外可能会粘在屏幕底部。因此,当您点击 ADD 时,继续将 textview 添加到该滚动视图中,您现在是如何做到的
  • @Raghavendra 这就是我两秒钟前所做的,哈哈!我将这些按钮放在了相对布局中的滚动视图之外(我将在上面更新我的布局文件)。现在,当我点击添加时,我需要一种方法将这五个文本视图打包成线性布局并将其放入滚动视图中。
  • 是的。制作一个包含 5 个 EditTexts 的模板 xml,然后使用 LayoutInflater 在单击按钮或类似操作时将它们添加到视图中。

标签: java android


【解决方案1】:

你可以在运行时通过代码添加这些EditText

【讨论】:

  • 也许让你的答案更详细一点......这并没有告诉 OP 任何事情。这是评论材料,不是回答材料
  • 谢谢您下次多多关照
【解决方案2】:

在你的 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);
    }
});

【讨论】:

  • 感谢您提供代码 sn-p。它有很大帮助。我只有一个问题。在我的情况下,dynamic-content.xml(我在问题中更新的最后一个 sn-p 代码)我有大约四个 EditText 视图,我将它们添加到我的滚动视图中的 LinearLayout 中。当用户点击添加按钮时。例如,如果单击添加按钮 5 次,那么屏幕上将有大约 25 个新的 EditText。如何访问用户在屏幕上出现的那些新 EditText 视图中键入的内容?由于这 5 个 EditText 中的每一个都代表一个 Price 对象(用户定义),我想将它们保存到数据库中。
  • @1290 您可以为每个膨胀的视图提供一个 ID,以便您可以引用它。另一种可能性是将您膨胀的所有内容添加到列表中,然后遍历它以获取所有值
  • @1290 这是我的代码中的一个示例:View row = getLayoutInflater().inflate(R.layout.someTemplate, someLayout, false); row.setId(someIntegerValue); 因此,您可以找到具有您提供的 ID 的模板,然后您可以从那里找到您需要的子视图
  • 因此,如果您有 4 个编辑文本,请在线性布局下获取 4 个文本视图,当他单击添加时阅读这些编辑文本,如果您愿意,可以将值添加到列表中,我做了类似的事情它可能有用 dynamicList.add(otheret.getText().toString().trim());\ textView1.setText(otheret.getText().toString().trim()); textView1.setId(textView1.getId() + 计数器); otheret.setText("");线性布局.addView(addView);
  • @1290 是的,那么包含LinearLayout 将获得您可以引用的Id。然后你可以在里面找到子视图:stackoverflow.com/questions/8395168/…
【解决方案3】:
    we can solve this, using List/Recycler view efficiently/easily 
    when ever you clicking on ADD add the item to list and notify the Recycler view adapter 

    and and removing also easy you just delete the item from list based on recycler View position.

    No need to create Dynamic View here recycler view will do the job


    // In Main Layout
    <android.support.v7.widget.RecyclerView
            android:id="@+id/recycleView_Stops"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/layoutSaveBottom"
            android:layout_below="@+id/ll1"
            android:layout_marginTop="@dimen/pad_10dp"
            app:layout_behavior="@string/bottom_sheet_behavior" />

    <TextView
                        android:id="@+id/tvAddOther"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:gravity="center"
                        android:text="@string/icon_plus"
                        android:textColor="@color/colorPrimary"
                        android:textSize="40sp"
                        app:customFontPath="@string/roboto_regular" />


    // design your layout item 

---------------------

and when clicking on Add set user entered details

addOther.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                RouteElements routeElements = new RouteElements();
                            routeElements.setLatitude(latitude);
                            routeElements.setLongitude(longitude);
                            routeElements.setStopName(otheret.getText().toString().trim());

                            routeElementsArrayList.add(routeElements);
                            routeStopsAdapter.notifyDataSetChanged();

        });

And in  Your Adapter for removing the item

public void remove(int position) {
            if (position < routeElementsArrayList.size()) {
                routeElementsArrayList.remove(position);
                notifyDataSetChanged();
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    相关资源
    最近更新 更多