2

我创建了一个可删除的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>

问题是,当我将更多这些放入一个布局并旋转到横向时,它们突然都具有相同的值。我想这是因为系统按 ID 恢复值,并且每个组件都由相同 ID 的元素组成。我怎么解决这个问题?

更多信息:

布局中的可删除EditText( )cz.kns.uome.component.CleanAndClearEditText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/cz.kns.uome"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ScrollView
        android:id="@+id/scrollView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:padding="5dp" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <cz.kns.uome.component.CleanAndClearEditText
                    android:id="@+id/nameEditText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_toLeftOf="@+id/contactPickerButton"
                    app:hint="@string/person_name"
                    app:type="textPersonName" />

                <ImageButton
                    android:id="@+id/contactPickerButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:contentDescription="@string/select_contact"
                    android:src="@drawable/ic_action_dropdown" />
            </RelativeLayout>

            <cz.kns.uome.component.CleanAndClearEditText
                android:id="@+id/emailEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                app:hint="@string/person_email_opt"
                app:type="textEmailAddress" />

            <cz.kns.uome.component.CleanAndClearEditText
                android:id="@+id/descriptionEditText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                app:hint="@string/description_opt"
                app:type="text" />

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <Button
                    android:id="@+id/saveButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="5"
                    android:text="@string/save" />

                <Button
                    android:id="@+id/cancelButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:text="@string/cancel" />
            </LinearLayout>
        </LinearLayout>
    </ScrollView>

</LinearLayout>

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
}

正常状态 旋转

4

3 回答 3

2

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

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 方法中从包中取回数据。

于 2013-07-31T00:27:43.160 回答
1

看看这是否有帮助:

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];
            }
        };
    }
}

和修改后的R.layout.clean_and_clear_edit_text文件:

<?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>

编辑: 代码的主要部分来自我作为评论发布的问题。

于 2012-05-19T13:52:29.407 回答
0

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

于 2012-05-19T11:43:51.327 回答