通过Android-XML自定义按钮单击事件

时间:2013-04-02 05:06:31

标签: android android-button android-custom-view

我在GitHub中创建了一个自定义的圆形图像按钮我有自己的clicklistener,如果触摸位于带有下面逻辑的圆形边界上,则会在onTouchEvent上调用

private void onTouchCircle(float x, float y, boolean up) {
    float cx = getWidth()/2;
    float cy = getHeight()/2;
    float distance = (float) Math.sqrt(Math.pow((x - cx), 2) + Math.pow((y - cy), 2));
    float radius = getWidth()/2;
    if (distance < radius  && !up) {
        IS_PRESSED = true;
        getDrawable().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0X2E2E2E));
        invalidate();
    } else {
        IS_PRESSED = false;
        getDrawable().setColorFilter(null);

        if(distance<radius){
            BUTTON_PRESSED=true;
        }else{
            BUTTON_PRESSED=false;
        }
        invalidate();
    }

    if(up && BUTTON_PRESSED){
        BUTTON_PRESSED=false;
        Log.e(TAG, "distance -- "+distance+" -- radius --"+radius);
        if(onClickListener!=null){
            onClickListener.onCircularButtonClick(this);
        }
    }

}

如何通过XML调用点击,如android.widget.Button / android.widget.ImageButton?如果我可以为我的自定义控件调用类似于button / imagebutton的方法,那就太好了。任何帮助表示赞赏。

<com.example.circularimageview.CircularImageView
        xmlns:imaginea="http://schemas.android.com/apk/res/com.example.circularimageview"
        android:id="@+id/imageView1"
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:scaleType="centerCrop"
        android:src="@drawable/breakdancing_android"
        imaginea:alpha="1"
        imaginea:onCircularButtonClick="myFancyMethod" />

public void myFancyMethod(View v) {
    // does something very interesting
}

1 个答案:

答案 0 :(得分:1)

了解Android如何在其基础View.java

中执行此操作

关键线是:

final String handlerName = a.getString(attr);
if (handlerName != null) {
    setOnCircularClickListener(new OnCircularClickListener() {
        private Method mHandler;

        public void onCircularButtonClick(View v) {
            if (mHandler == null) {
                try {
                    mHandler = getContext().getClass().getMethod(handlerName,
                            View.class);
                } catch (NoSuchMethodException e) {
                    int id = getId();
                    String idText = id == NO_ID ? "" : " with id '"
                            + getContext().getResources().getResourceEntryName(
                                id) + "'";
                    throw new IllegalStateException("Could not find a method " +
                            handlerName + "(View) in the activity "
                            + getContext().getClass() + " for onClick handler"
                            + " on view " + CircularImageView.this.getClass() + idText, e);
                }
            }
            try {
                mHandler.invoke(getContext(), CircularImageView.this);
            } catch (IllegalAccessException e) {
                throw new IllegalStateException("Could not execute non "
                        + "public method of the activity", e);
            } catch (InvocationTargetException e) {
                throw new IllegalStateException("Could not execute "
                        + "method of the activity", e);
            }
    });
}