首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过Android-XML定制按钮点击事件

通过Android-XML定制按钮点击事件
EN

Stack Overflow用户
提问于 2013-04-02 13:06:31
回答 1查看 877关注 0票数 0

我已经在GitHub中创建了一个自定义的圆形图像按钮,我有自己的点击侦听器,如果触摸是在圆形边界上,则会使用下面的逻辑调用onTouchEvent

代码语言:javascript
复制
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);
        }
    }

}

如何像android.widget.Button/android.widget.ImageButton?一样通过XML调用单击操作?如果我能为我的自定义控件调用类似于button/imagebutton的方法,那就太好了,如下所示。任何帮助都是非常感谢的。

代码语言:javascript
复制
<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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-02 13:56:19

看看安卓是如何在他们的基础View.java中做到这一点的

关键的几行代码是:

代码语言:javascript
复制
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);
            }
    });
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15756515

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档