2016-11-15 128 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(); 
      } 
     }