2016-11-15 130 views
1

解决下面的代码后,显示更多的意见WORKS安卓:如何点击一个按钮

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

enter image description here

这五个临时EditTexts代表用户可以输入的一些信息(如项目的价格,订单号码等)。如果用户想要添加另一个项目,他们将点击添加按钮,并且我希望在屏幕右上方的屏幕上出现另外5个文本视图两个按钮,但在前一组下方的5 EditTexts。有人能给我一个关于如何做到这一点的起点。

我片段的布局是这样的:

  • 我有一个顶层的线性布局(垂直方向)。
  • 然后滚动查看。
  • 在scrollview中我有另一个线性布局。
  • 在该线性布局我有这五个的EditText目的和两个按钮

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

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

当我动态地添加新的意见,我做什么,我想添加,然后通过添加它的模板'LayoutInflater' – Aidin

+0

哦确定,所以你说使用' LayoutInflater'将其添加到'scrollview'中? – CapturedTree

+0

@ 1290试试把这些按钮放在scrollview的外面,可能会粘在屏幕的底部。所以,当你点击ADD继续添加textview到那个滚动视图如何你现在已经这么做 – Raghavendra

回答

2

在XML中添加这些EditText上采取

<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(它会动态地添加你的时候点击ADD) dynamic_item.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); 
    } 
}); 
+0

谢谢你的代码片段。它帮助了很多。我只有一个问题。在我的例子中,dynamic-content.xml(在我的问题中更新了代码的最后一个片段)中,我有大约四个EditText视图添加到我的滚动视图中的LinearLayout。当用户点击添加按钮时。例如,如果添加按钮被单击5次,那么屏幕上会出现约25个新的EditText。我将如何访问用户在屏幕上出现的新EditText视图中键入的内容?由于这5个EditText中的每一个代表一个Price对象(用户定义的),我想将它们保存到数据库中。 – CapturedTree

+1

@ 1290您可以给每个您膨胀的视图提供一个ID,以便您可以参考。另一种可能性是将你膨胀的所有东西添加到列表中,然后遍历它以获取所有值 – Aidin

+0

@Aidin当我使用'LayoutInflator'动态添加它时,来自XML文件的视图对象具有相同的值我为该布局文件的每个实例创建了“Id's”。那么是否有一种方法可以让每个View对象id对于我为'dynamic-content.xml'制作的每个实例都是唯一的? – CapturedTree

-1

您可以通过代码在运行时

+2

也许让您的回答稍微更精细一些......这不会告诉OP任何事情。这是评论材料,而不是回答材料 – Aidin

+0

谢谢你从下次开始谨慎 –

0
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(); 
      } 
     }