사용자 정의보기를 만들었으므로 사용자 정의 속성을 설정하고 싶습니다. 다른보기의 ID를 특성으로 전달하고 싶습니다.사용자 정의 속성이있는 Android 사용자 정의보기
사용자 정의보기 바인드합니다 : 난 내 사용자 정의보기 사용
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="IdNumber">
<attr name="firstName" format="integer"/>
<attr name="lastName" format="integer"/>
<attr name="address" format="integer"/>
<attr name="birthDate" format="integer"/>
</declare-styleable>
</resources>
레이아웃 :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/display_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_centerHorizontal="true"
android:tag="name"
android:ems="10" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/id_number"
android:layout_centerHorizontal="true"
android:layout_below="@id/display_name"/>
<ge.altasoft.custom_views.IdNumber
android:id="@+id/custom_id_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@id/id_number"
android:paddingLeft="35dip"
custom:firstName="@id/display_name"/>
</RelativeLayout>
내가 속성 값을 얻으려면 사용자 정의보기 클래스의 생성자 :
public IdNumber (Context context, AttributeSet attrs) {
super(context, attrs);
initViews();
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.IdNumber);
final int N = a.getIndexCount();
for(int i = 0; i < N; i++){
int attr = a.getIndex(i);
switch(attr){
case R.styleable.IdNumber_firstName:
int firstNameViewID = a.getInteger(attr, -1);
break;
}
}
a.recycle();
}
문제는 int firstNameViewID = a.getInteger(attr, -1);
은보기의 ID가 아니라 단지 0입니다.
custom:firstName="@id/display_name"
< < < 여기에 잘못된 것이 틀림 없습니다. 그러나 무엇이 잘못되었는지는 알 수 없습니다. 사용자 지정 특성에 일부 정수 값을 할당하면 작동하지만 Id-s에서는 작동하지 않습니다.
사전에 협조 해 주셔서 감사합니다.
도움을 주셔서 감사합니다. – Jilberta