선형 레이아웃을 확장하는 사용자 정의 위젯이 있습니다. litle로 된 텍스트 블록입니다. 여기커스텀 위젯의 커스텀 onClickListner - 어떻게?
위젯의 XML입니다 :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:background="@drawable/actionbar_background"
android:layout_height="35dp"
android:paddingLeft="15dp"
android:gravity="center_vertical">
<TextView
android:id="@+id/title"
android:layout_height="35dp"
android:textColor="@color/white"
android:layout_width="match_parent"
android:textSize="18dp"
android:clickable="true"
android:gravity="center_vertical" />
</LinearLayout>
<TextView
android:id="@+id/ingreds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Some text"
android:paddingLeft="15dp" />
</LinearLayout>
난 그 사용자 정의 위젯 onClickListener
을 만드는 경우 어딘가에 Activity
에서 - 내가 그 레이아웃에 마지막 TextView
클릭하면에만 반응한다. 하지만 "title"textView가 포함 된 가로 LinearLayout
또는 제목 TextView에 직접 onClickListener를 설정해야합니다. 중요 : 이러한 수신기를 설정하려고합니다. OWTSIDE 사용자 정의 위젯 클래스.
내 맞춤 클래스 또는 그와 비슷한 것으로 setOnclickListener
메서드를 재정의해야한다고 생각하지만 어떻게 처리 할 수 있는지 잘 모르겠습니다. 응답에 대한
public class MyTextBlock extends LinearLayout {
private TextView title;
private TextView ingreds;
public MyTextBlock(Context context) {
this(context, null);
}
public MyTextBlock(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.my_text_block, this, true);
title = (TextView) findViewById(R.id.title);
ingreds = (TextView) findViewById(R.id.ingreds);
}
public void setText(String text) {
ingreds.setText(text);
}
public void setTitle(String titleText) {
title.setText(titleText);
}
}
감사 : 여기
사용자 정의 위젯의 클래스입니다.
그래서 맞춤 이벤트 수신기를 만들어서이 문제를 해결합니다. http://stackoverflow.com/questions/8292712/android-custom-event-listener/8293106#8293106 – udenfox