使用覆盖 onclick 创建自定义控件 [英] Creating custom control with overriding onclick

查看:65
本文介绍了使用覆盖 onclick 创建自定义控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 android 应用程序,我有一个扩展 TextView 的自定义 GUI 组件.

I am working on an android app and I have a custom GUI component which extends a TextView.

当从我的自定义控件类和重写的 onclick 方法中单击时,我想让我的自定义控件执行一项任务.

I want to have my custom control do a task when clicked from my custom control class and my overridden onclick method.

例如,我的扩展 TextView 的类实现了 OnClick 侦听器并将日志写入日志猫.

For example my class that extends the TextView implements the OnClick listener and writes a log to the log cat.

然后在我的活动中,我为我的自定义控件设置了一个 onclick 侦听器,这显示了一个 Toast 通知.

Then in my activity, I set an onclick listener to my custom control, and this shows a toast notification.

我想要发生的是,当我的自定义控件被点击时,我被覆盖的 onclick 活动显示 toast 并且点击方法上的自定义控件类也被运行以显示日志.但是我似乎只能让一个工作或另一个工作,例如,如果我不运行 myCustom.setOnClickListener(myListener) 然后使用类 onclick 并记录日志,如果我设置了 onClick 侦听器,那么我只会得到吐司不是日志.

What I want to happen, is when my custom control is clicked, my activities overridden onclick shows the toast and the custom control class on click method also is run to show the log. But I can only seem to get one working or the other, for example, if I don't run myCustom.setOnClickListener(myListener) then the classes onclick is used and does the log, if I set the onClick listener then I only get the toast not the log.

下面是我的自定义控件类

Below is my custom control class

public class NavTextView extends TextView implements View.OnClickListener
{
    public NavTextView(Context context) {
        super(context);
        setOnClickListener(this);
    }

    public NavTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOnClickListener(this);
    }

    public NavTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOnClickListener(this);
    }

    public NavTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        Log.d("NavTextView", "This has been clicked");
    }
}

下面是我的活动 onCreate 方法

Below is my activities onCreate method

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        navTextView = (NavTextView)findViewById(R.id.navTextView);

        navTextView.setOnClickListener(mClickListener);
    }

希望这是有道理的

推荐答案

一个 View 只能有一个 OnClickListener.在您的 NavTextView 中,您将其设置在那里.如果您稍后再次调用 setOnClickListener,您将替换之前的侦听器.

A View can only have one OnClickListener. In your NavTextView you are setting it there. If you later call setOnClickListener again, you are replacing the previous listener.

您可以做的是在您的自定义 View 中覆盖 setOnClickListener,然后包装 OnClickListener 并调用两者.

What you can do is override setOnClickListener in your custom View, then wrap the OnClickListener and call both.

public class MyTextView extends TextView implements View.OnClickListener
{
    OnClickListener _wrappedOnClickListener;

    public MyTextView(Context context) {
        super(context);
        super.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        Log.d("NavTextView", "This has been clicked");

        if (_wrappedOnClickListener != null)
            _wrappedOnClickListener.onClick(view);
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        _wrappedOnClickListener = l;
    }
}

这篇关于使用覆盖 onclick 创建自定义控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆