2015-09-16 29 views
0

如果你一步一步做下去(如下所述),你會理解我的問題(抱歉,但我不能讓你以其他方式理解)。這可能會讓人困惑。但是,我相信如果你讀過一次,你就會明白。定製LinearLayout與EditText裏面片段

  1. 創建一個新的android項目,然後創建兩個碎片。你現在會有 有

    a。 MainActivity.java。

    b。 FirstFragment(FirstFragment.java)與它的xml 文件(first_fragment.xml)。

    c。 SecondFragment(SecondFragment.java)與它的xml 文件(second_fragment.xml)。

現在創建一個自定義的LinearLayout類(FormRow.java)如下:

public class FormRowNew extends LinearLayout { 
    public FormRowNew(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initView(); 
    } 

    private void initView() { 
     View.inflate(getContext(), R.layout.form_row, this); 
    } 
} 

XML文件(form_row)本是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout  
     xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

    <EditText 
     android:id="@+id/editText" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="eeeee"/> 
</LinearLayout> 

使用這個定製的線性類TWICE裏面FirstFragment如下:

<gallery.com.yyyyyyyyy.FormRow 
    android:id="@+id/first" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/txvFirst" 
    > 
</gallery.com.yyyyyyyyy.FormRow> 

<gallery.com.yyyyyyyyy.FormRow 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/first" 
    > 
</gallery.com.yyyyyyyyy.FormRow> 

請記住:我已經使用了兩次。您也至少使用它。

現在,在下面的某個id(rlRootLayout)中加載MainActivity.java中的FirstFragment。

FirstFragment firstFragment = new FirstFragment(); 
getSupportFragmentManager().beginTransaction().addToBackStack(null)                  
.replace(R.id.rlRootLayout, firstFragment).commit(); 

類似地,負載第二塊碎片從FirstFragment相同ID(rlRootLayout)像的下方。

secondFragment blankFragment = new secondFragment(); 
getActivity().getSupportFragmentManager().beginTransaction() 
.addToBackS tack(null).replace(R.id.rlRootLayout, blankFragment) 
.commit(); 

我在這裏從FirstFragment 在ID(rlRootLayout)加入到SecondFragment MainActivity。

問題: - 運行應用程序,並提到轉到頁:

MainActivity -> 
FirstFragment -> 
Change something in BOTTOM FormRow(like add some text in EditText) -> 
SecondFragment -> 
FirstFragment. 

現在,見裏面FirstFragment自定義類(FormRow)。 無論我們在底部FormRow類中添加了什麼,都會自動添加到頂部FormRow中。我無法找到發生這種情況的原因。即使我對頂級FormRow沒有任何幫助,當我從SecondFragment返回時,它也會根據底部變化。 請幫忙。

回答

1

將文本保存在editText中,同時添加第二個片段。然後設置保存的文本,同時返回第一個從此調用@Override public void onViewStateRestored(@Nullable Bundle savedInstanceState) { super.onViewStateRestored(savedInstanceState); //set edit text }

+0

thnx分配相同的值。 –

+0

這個幫我@Arjun –

0

該行爲是因爲EditText的ID。當第一個片段在從第二個片段返回時對佈局進行膨脹時,它會使用它分配編輯文本的值。所以在這種情況下,兩個EditText的ID都是相同的,所以它爲這兩個Edittext的

+0

Thanx尋求幫助。 –