2016-02-05 149 views
4

我已经创建了自定义按钮并尝试使用 @onClick注释,但它不起作用。ButterKnife onClick自定义视图

是否可以使用这样的自定义视图或我将不得不使用默认onClickListener

的CustomButton

public class CustomButton extends RelativeLayout { 

public CustomButton(Context context) { 
    super(context); 
} 

public CustomButton(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(); 
} 

public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(); 
} 

@TargetApi(Build.VERSION_CODES.LOLLIPOP) 
public CustomButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    init(); 
} 

private void init() { 
    inflate(getContext(), R.layout.button_layout, this); 
} 

菜单

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    final View rootView = inflater.inflate(R.layout.fragment_menu, container, false); 
    ButterKnife.bind(this, rootView); 
    return rootView; 
} 

@OnClick(R.id.button_play) 
public void onButtonPlayClicked() { 
    // NOT CALLED 
} 

菜单布局

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/bg_pattern" 
android:padding="15dp"> 

<com.example.ui.custom.CustomButton 
    android:id="@+id/button_play" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"/> 
</RelativeLayout> 
+0

你使用Eclipse还是Android Studio? –

+0

我正在使用Android Studio – cRobot

回答

2

http://jakewharton.github.io/butterknife/

自定义视图可以通过未指定ID绑定到其自己的侦听器。

public class FancyButton extends Button { 
    @OnClick 
    public void onClick() { 
     // TODO do something! 
    } 
} 

希望它可以帮助!

+0

谢谢,但这不起作用。我需要在活动 – cRobot

-1
In your activity try to add.. 

ButterKnife.inject(this); 
check this code 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
ButterKnife.inject(this); 
} 

@OnClick(R.id.buttonAlert) 
public void alertClicked(View v){ 
new AlertDialog.Builder(v.getContext()) 
    .setMessage(getFormattedMessge()) 
    .setNeutralButton("OK", new DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     dialog.dismiss(); 
     } 
    }) 
    .show(); 
} 
+0

的CustomButton上使用'@ onClick'此代码适用于默认视图而非自定义视图。不幸 – cRobot

2

我遇到过同样的问题。我通过在自定义视图中处理事件调度来解决问题。

你的情况:

public class CustomButton extends RelativeLayout { 

    // ... 

    @Override 
    public boolean dispatchTouchEvent(MotionEvent event) { 
     if(event.getAction() == MotionEvent.ACTION_UP) { 
      if(hasOnClickListeners()) { 
       callOnClick(); 
      } 
     } 
     return super.dispatchTouchEvent(event); 
    } 

    // ... 

} 

希望这可以帮助。

+1

这应该被接受的答案,我有同样的问题,OP和你的代码和平解决了问题... –

3

目前,我遇到同样的问题,并找到了一个非常好的解决方案。 我在我的视图中覆盖了setOnClickListener(OnClickListener l)并将侦听器指定给我的按钮。之后,@OnClick按预期工作。

就您的情况您可以添加下一个方法:

@Override 
public void setOnClickListener(OnClickLister l) { 
    ButterKnife.findById(this.getView(), R.id.button_play).setOnClickLister(l); 
} 
2

我有同样的问题,我找到了解决!

替换您的自定义视图的XML(R.layout.button_layout)使用<merge>而不是根视图。正如你可以在ButterKnife文档中看到的:

使用ButterKnife.bind(this)将视图的子项绑定到字段中。 如果在布局中使用<merge>标记并在自定义视图构造函数中膨胀,则可以在之后立即调用此标记。或者,从XML充入的自定义视图类型可以在onFinishInflate()回调中使用它。

在我来说,我代替:

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/bg_button_default" 
android:clickable="true" 
android:focusable="true" 
android:padding="@dimen/normal_padding" 
tools:background="@color/white" 
tools:layout_height="80dp"> 

通过:

<merge 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools"> 

现在,父视图配置是CustomView初始化方法里面。所有的OnClick和OnLongClick活动都在运作!

希望这可以帮助!

+1

是的这项工作!但我也添加了代码setClickable(true);在自定义视图类中。因为我需要clieckable根视图 –