Android Compound Controls相互影响

时间:2016-02-27 17:12:08

标签: android android-fragments custom-controls

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

我的复合控件扩展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" />

然后,如果我在EditText中的edittext_label_1control_1)中输入文字,并在EditText中的edittext_label_1control_2)中输入文字}。然后,当我旋转设备时,我在control_2输入的文字现在同时显示在control_1和control_2`中。

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

1 个答案:

答案 0 :(得分:0)

原来有一个相同的问题。通过以下链接中的所选答案完美解决:

Android: embedded controls in multiply included compound control restore to same state