我想点击添加到我的自定义项目:点击自定义项与Android的butterknife
public class ContactItem extends FrameLayout {
@BindView(R.id.itemHeader)
TextView textLabel;
@BindView(R.id.itemValue)
TextView textValue;
@BindView(R.id.imageIcon)
ImageView icon;
@BindView(R.id.mainLayout)
RelativeLayout mainLayout;
private String label = null;
public ContactItem(Context context) {
super(context);
init(context, null);
}
public ContactItem(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public ContactItem(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null) {
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.textLabel);
label = a.getString(R.styleable.textLabel_text_attr);
}
addView(inflate(context, R.layout.item_contact, null));
ButterKnife.bind(this);
}
public TextView getTextLabel() {
return textLabel;
}
public TextView getTextValue(){
return textValue;
}
public ImageView getIcon(){
return icon;
}
public void setTextLabelText(int text){
textLabel.setText(text);
}
public void setTextValueText(String text){
textValue.setText(text);
}
public void setIconRes(int res){
icon.setImageResource(res);
}
}
在我的活动我加几件这种类型。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/color_white">
<TextView
android:id="@+id/Header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="15dp"
android:paddingTop="20dp"
android:textAllCaps="true"
android:textStyle="bold"
android:background="@color/background"/>
<views.items.ContactItem
android:id="@+id/contactTelefon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
<views.items.ContactItem
android:id="@+id/contactEmail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"/>
</LinearLayout>
现在,如果我点击项目contactEmail我要发电子邮件,如果contactTelefon我要打个电话。我有功能,但onClick不起作用。有任何想法吗?
@OnClick(R.id.contactTelefon)
public void clickOnCall(){
presenter.callFunction());
}
没有结果... :( – edi233
@ edi233我编辑了我的答案 –