【问题标题】:Custom View which has subviews with same id具有相同 ID 的子视图的自定义视图
【发布时间】:2013-02-06 21:11:37
【问题描述】:

我已经实现了一个自定义视图,其中包含两个由 xml 中的 id 标识的子视图。在同一布局中使用两个这样的自定义视图时,我遇到了选择哪个自定义视图是随机的问题。

如何编写具有不同视图 ID 且可在同一布局中多次使用的自定义视图?

这是自定义视图的xml:

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

<EditText
    android:id="@+id/clearable_edit"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inputType="textCapWords"
    android:paddingRight="35dip" />

<Button
    android:id="@+id/clearable_button_clear"
    android:layout_width="30dip"
    android:layout_height="30dip"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dip"
    android:background="@drawable/clear_button" />

</RelativeLayout>

EditText 的 id (android:id="@+id/clearable_edit") 是这里的问题。

自定义视图的使用:

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

            <com.custom.package.ClearableEditText
                android:id="@+id/arr_location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 >
            </com.custom.package.ClearableEditText>
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <com.custom.package.ClearableEditText
                android:id="@+id/dep_location"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                 >
            </com.custom.package.ClearableEditText>
        </LinearLayout>

在此示例中,“ClearableEditText”类型的视图共享其 EditText 子视图的相同 ID。

这是 ClearableEditText 的代码:

public class ClearableEditText extends RelativeLayout {

private LayoutInflater inflater = null;
private EditText edit_text;
private Button btn_clear;

public ClearableEditText(Context context, AttributeSet attrs, int defStyle){
    super(context, attrs, defStyle);
    initViews();
}

public ClearableEditText(Context context, AttributeSet attrs){
    super(context, attrs);
    initViews();

}

public ClearableEditText(Context context){
    super(context);
    initViews();
}

private void initViews(){
    inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.clearable_edittext, this, true);
    edit_text = (EditText) view.findViewById(R.id.clearable_edit);
    btn_clear = (Button) findViewById(R.id.clearable_button_clear);
    btn_clear.setVisibility(RelativeLayout.INVISIBLE);
}
}

【问题讨论】:

    标签: android layout


    【解决方案1】:

    首先像这样获取父级View

    View v1 = findViewById(R.id.arr_location);
    

    然后

    EditText ed1 = (EditText)v1.findViewById(R.id.clearable_edit);
    

    同样

    View v2 = findViewById(R.id.dep_location);
    EditText ed2 = (EditText)v2.findViewById(R.id.clearable_edit);
    

    这样,您可以添加任意数量的ClearableEditText,使EditTextButton 具有相同的ID。只需确保每个 ClearableEditText 都有不同的 id,例如在本例中为 R.id.arr_locationR.id.dep_location

    【讨论】:

    • 这对我来说没有多大帮助。我的问题是,在使用我的两个 ClearableEditText 更改片段时,它们的状态会混合在一起。这意味着有时 editText 1 的文本在 editText 2 中,反之亦然。在我看来,其原因是在自定义视图中使用了相同的 id (R.id.clearable_edit)。
    【解决方案2】:

    我找到了解决办法。

    我向 ClearableEditText 添加了一个方法,您可以在其中从对象外部设置底层 EditText 的 id 并使用新的 id 设置它。

    这是一个代码示例:

    //inside ClearableEditText
    public void setEditId(int id){
        edit_text.setId(id);
    }
    
    //somewhere else
    
    departureLocation = (ClearableEditText)view.findViewById(R.id.dep_location);
    departureLocation.setEditId(R.id.clearable1);
    arrivalLocation = (ClearableEditText)view.findViewById(R.id.arr_location);         
    arrivalLocation.setEditId(R.id.clearable2);
    

    ID 是在 values 文件夹中使用“ids.xml”创建的,这会导致 eclipse/ADT 为输入的项目创建一个占位符 id

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <!-- This file is used to create unique ids for custom views, which will be used more 
       than once in the same layout file. Using unique ids prevents the custom view from         getting 
       the wrong state. -->
            <item name="clearable1" type="id"></item>
            <item name="clearable2" type="id"></item>
    </resources>
    

    【讨论】:

    • 此处讨论的用于修改 id 的更具扩展性的解决方案:stackoverflow.com/q/20405526/1438339。您可以从ClearableEditText initViews 内部调用setId。在你得到edit_textfindViewById 之后,就像edit_text.setId(getId() + R.id.clearable_edit) 之类的东西
    猜你喜欢
    • 2016-07-10
    • 2011-11-30
    • 1970-01-01
    • 2012-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多