我創建了一個自定義按鈕,該按鈕從XML佈局中膨脹。一切工作正常,除了沒有觸發點擊偵聽器。
我懷疑問題是因爲android:clickable="true"
屬性,因爲當我刪除它的點擊偵聽器被觸發。但我需要設置這個屬性,因爲我的自定義視圖使用選擇器作爲背景,如果我刪除它,那麼選擇器將不再工作。未觸發自定義視圖的OnClickListener
這裏的類定義:
public class CustomButton extends LinearLayout{
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_button, this, true);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomButton, 0, 0);
String titleStr = a.getString(R.styleable.CustomButton_title);
String subTitleStr = a.getString(R.styleable.CustomButton_subTitle);
int iconResId = a.getResourceId(R.styleable.CustomButton_icon, R.drawable.ic_launcher);
a.recycle();
TextView title = (TextView)findViewById(R.id.title);
title.setText(titleStr);
TextView subTitle = (TextView)findViewById(R.id.subTitle);
subTitle.setText(subTitleStr);
ImageView icon = (ImageView)findViewById(R.id.icon);
icon.setImageResource(iconResId);
}
}
的XML佈局,自定義視圖從充氣:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/white_box"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/main_button_selector"
android:clickable="true"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="icon"
android:layout_marginLeft="5dip"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/grey_text"
android:textSize="@dimen/mainTextSize"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/subTitle"
android:ellipsize="end"
android:maxLines="2"
android:textColor="@color/grey"
android:textSize="@dimen/mainSubTextSize"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
這裏就是我如何使用它在XML佈局:
<com.customviews.CustomButton
android:id="@+id/pay"
android:layout_weight="1"
android:layout_width="0dip"
android:layout_height="fill_parent"
custom:title="Hello World"
custom:subTitle="Subtitle"
custom:icon="@drawable/ic_launcher"/>
以及活動如何設置點擊收聽者:
CustomButton button = (CustomButton) findViewById(R.id.pay);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyActivity.this, "Hello World!", Toast.LENGTH_LONG).show();
我已經看到幾個線程已經解決了這個問題,但他們沒有幫助我。將不勝感激任何幫助。
您的CustomButton擴展了LinearLayout。你正在嘗試爲此設置一個onClickListener。是你想要的東西嗎?? – SKK
是的,類似的東西。但同時CustomButton有一個背景選擇器。 –
我無法在任何地方看到button.setclickable(true),這是否正常? – wazaminator