具有子视图具有相同ID的自定义视图 [英] Custom View which has subviews with same id

查看:83
本文介绍了具有子视图具有相同ID的自定义视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个自定义视图,其中包含两个子视图,这些子视图由xml中的ID标识.当在同一布局中使用两个自定义视图时,我碰到一个问题,即选择哪个自定义视图是随机的.

I've implemented a custom view with hosts two subviews which are identified by an id in the xml. When using two of this custom view in the same layout I run into the problem that it is random which custom view is chosen.

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

How can I write a custom view with different view ids that can be multiply used in the same layout?

这是自定义视图的xml:

Here is the xml of the custom view:

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

The id (android:id="@+id/clearable_edit") of the EditText is the problem here.

自定义视图的用途:

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

In this example the views of type "ClearableEditText" share the same id of their EditText subview.

这是ClearableEditText的代码:

Here is the code for 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);
}
}

推荐答案

我找到了解决方案.

我在ClearableEditText中添加了一个方法,您可以在其中从对象外部设置基础EditText的ID,并为其设置新的ID.

I've added a method to ClearableEditText where you can set the id of the underlying EditText from outside the object and set it with a new 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);

在值文件夹中使用"ids.xml"创建ID,这会导致eclipse/ADT为输入的项目创建占位符ID.

The Ids are created with a "ids.xml" in the values folder, which causes eclipse/ADT to create a placeholder id for the entered items

<?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的自定义视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆