Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/374.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当一个布局中有多个具有相同ID的组件时,旋转后的值错误_Java_Android_Android Layout - Fatal编程技术网

Java 当一个布局中有多个具有相同ID的组件时,旋转后的值错误

Java 当一个布局中有多个具有相同ID的组件时,旋转后的值错误,java,android,android-layout,Java,Android,Android Layout,我创建了一个deletableEditText。这是布局图 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layou

我创建了一个deletable
EditText
。这是布局图

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

<EditText
    android:id="@+id/cacEditText"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="text"
    android:paddingRight="35dp" />

<Button
    android:id="@+id/cacClearButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_marginLeft="20dp"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:background="@android:drawable/ic_input_delete"
    android:visibility="invisible" />

</RelativeLayout>
java类

public class CleanAndClearEditText extends RelativeLayout {

    private final EditText editText;
    private final Button clearButton;

    public CleanAndClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.clean_and_clear_edit_text, this);

        TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
                R.styleable.ClearAndCleanEditText);

        // ?????
        // I have to somehow access that edittext here but static id does not work
        editText = (EditText) findViewById(R.id.cacEditText);
        editText.addTextChangedListener(textWatcher);
        editText.setHint(typedArray.getString(R.styleable.ClearAndCleanEditText_hint));

        String type = typedArray.getString(R.styleable.ClearAndCleanEditText_type);
        if (type != null) {
            editText.setInputType(InputTypes.get(type));
        }

        clearButton = (Button) findViewById(R.id.cacClearButton);
        clearButton.setOnClickListener(clearButtonListener);
    }
    // rest ommited
}

好的,我看到您正在扩展自定义代码并将其插入到线性布局中。您应该使用ListView并实现ListAdapter。您可以自定义ListItem以满足您的需要。 我会这样做。

看看这是否有帮助:

public class CleanAndClearEditText extends RelativeLayout {

    private final EditText editText;
    private final Button clearButton;

    public CleanAndClearEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.clean_and_clear_edit_text, this);
        RelativeLayout rl = (RelativeLayout) getChildAt(0); // consider using a merge tag in R.layout.clean_and_clear_edit_text instead of the parent RelativeLayout
        //clearButton = (Button) findViewById(R.id.cacClearButton);
        clearButton = (Button) rl.getChildAt(1);
        clearButton.setOnClickListener(null);
        //editText = (EditText) findViewById(R.id.cacEditText);
        editText = (EditText) rl.getChildAt(0);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
        ss.stateToSave = editText.getText().toString();
        return ss;

    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (!(state instanceof SavedState)) {
            super.onRestoreInstanceState(state);
            return;
        }
        SavedState ss = (SavedState) state;
        super.onRestoreInstanceState(ss.getSuperState());
        editText.setText(ss.stateToSave);
    }

    static class SavedState extends BaseSavedState {
        String stateToSave;

        SavedState(Parcelable superState) {
            super(superState);
        }

        private SavedState(Parcel in) {
            super(in);
            stateToSave = in.readString();
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeString(stateToSave);
        }

        // required field that makes Parcelables from a Parcel
        public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }

            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }
        };
    }
}
编辑:
代码的主要部分来自我作为评论发布的问题。

我正在处理这个问题,我能够“解决”这个问题,通过删除布局中包含自定义视图的所有视图,实现以下功能:

public void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    // retain your data in the bundle then remove views
    layout.removeAllViewsInLayout();
}

因此,您可以使用onCreate方法从包中取回数据。

我不理解您的问题。如果您在一个版面中放置了更多这些内容,您应该有不同的id。请查看更新的问题您必须为自定义
视图保存状态。这里有一个详细答案的链接。@Luksprog这不是问题所在。如果我使用它,我就不必创建自己的组件,因为它根本帮不上我的忙,我刚刚经历了同样的现象。“你能进一步解释一下为什么会发生这种情况吗,@Tomas?”?这仅仅是因为您膨胀的xml布局中的ID吗?您可以将不同的视图添加到listview中。listview没有用,这不是组件列表。。。这是一个可重用的组件,可以在任何地方使用。我试图手动设置ID,但效果很好,但有点不好。这个更长,但更好更安全。非常感谢。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal" >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="text"
        android:paddingRight="35dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="5dp"
        android:layout_marginTop="5dp"
        android:background="@android:drawable/ic_input_delete"
        android:visibility="invisible" />

</RelativeLayout>
public void onSaveInstanceState(Bundle outState) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState(outState);
    // retain your data in the bundle then remove views
    layout.removeAllViewsInLayout();
}