2016-02-27 83 views
0

我在使用创建的复合控件的多个版本时遇到问题。当我在同一个Fragment中使用它两次时,我在第一个控件的EditText字段中输入的任何文本将替换为我在第二个控件旋转后在第二个控件的EditText中输入的文本。我的控制权的两个版本似乎互相影响。影响彼此的Android复合控件

我的复合控制延伸LinearLayout和如下:

public class MyCompoundControl extends LinearLayout { 

    public MyCompoundControl(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     inflater.inflate(R.layout.layout_my_compound_control, this, true); 

     TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyControlViewOptions); 
     String value1 = a.getString(R.styleable.MyControlViewOptions_firstvalue); 
     String value2 = a.getString(R.styleable.MyControlViewOptions_secondvalue); 
     a.recycle(); 

     EditText editText1 = (EditText) findViewById(R.id.edittext_label_1); 
     EditText editText2 = (EditText) findViewById(R.id.edittext_label_2); 
     TextView textView1 = (TextView) findViewById(R.id.textview_label_1); 
     TextView textView2 = (TextView) findViewById(R.id.textview_label_2); 

     textView1.setText(value1); 
     textView2.setText(value2); 
    } 
} 

下面是我layout.xml文件,它使用一个合并的复合控件:

<?xml version="1.0" encoding="utf-8"?> 
     <merge xmlns:android="http://schemas.android.com/apk/res/android">  
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
       <TextView 
        android:id="@+id/textview_label_1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
       <EditText 
        android:id="@+id/edittext_label_1" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:inputType="numberDecimal" /> 
      </LinearLayout> 
      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
       <TextView 
        android:id="@+id/textview_label_2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" /> 
       <EditText 
        android:id="@+id/edittext_label_2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:inputType="numberDecimal" /> 
      </LinearLayout> 
     </merge> 

然后我用这个控制2x在我的片段布局

xmlns:mycustom="http://schemas.android.com/apk/res-auto" 

    ... 

    <com.android.mytracker.custom.MyCompoundControl 
     android:id="@+id/control_1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     mycustom:firstvalue="weight1" 
     mycustom:secondvalue="weight2" /> 

    <com.android.mytracker.custom.MyCompoundControl 
     android:id="@+id/control_2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     mycustom:firstvalue="height1" 
     mycustom:secondvalue="height2" /> 

然后,如果我在文本中EditTextedittext_label_1)在control_1类型和control_2EditTextedittext_label_1)键入文本。然后,当我旋转我的设备时,我在control_2中输入的文本现在显示在control_1和control_2`中。

这绝对不是我的代码这样做。如果我然后在上面的片段布局(例如control_2然后control_1)中切换我的复合控件的顺序,则它切换。我首先在我的片段布局中放置的控件的旋转设备时,其控件的值将覆盖其EditText值。

回答