如何将 CheckedChange 监听器传递给自定义视图

How to pass a CheckedChange Listener to a custom view

更新于 2020/02/17 好的,我找到了解决方案。自定义侦听器在自定义 class 中停止工作的原因是 XML 文件布局。过去,我所有的成功经验都是建立在LinearLayout之上的。我在最初的问题描述中遗漏了这一点。新的自定义视图 XML 文件我使用了 ConstraintLayout,而不是 LinearLayout。这会导致用于 LinearLayout 的 'abnormal' 膨胀行为。

在LinearLayout自定义视图class中,我使用了init()函数

inflate(getContext(), R.layout.my_ll_custom_view,this);

这不适用于约束布局。 我必须使用以下代码,

View rootView = LayoutInflater.from(context).inflate(R.layout.my_cl_custom_view, this, false);
ConstraintSet constraintSet = new ConstraintSet();
this.addView(rootView);
ConstraintLayout.LayoutParams clp = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(clp);
constraintSet.clone(this);
constraintSet.connect(rootView.getId(), ConstraintSet.LEFT, this.getId(), ConstraintSet.LEFT, 0);
constraintSet.connect(rootView.getId(), ConstraintSet.RIGHT, this.getId(), ConstraintSet.RIGHT, 0);
constraintSet.connect(rootView.getId(), ConstraintSet.TOP, this.getId(), ConstraintSet.TOP, 0);
constraintSet.connect(rootView.getId(), ConstraintSet.BOTTOM, this.getId(), ConstraintSet.BOTTOM, 0);

可以在https://jordan-dixon.com/2017/12/30/custom-views-with-constraint-layout-and-kotlin/

中找到更多详细信息

我读了这个博客来弄清楚这个问题。我很感激。

======原问题描述从这里开始======

我制作了一个带有 3 个切换按钮的自定义视图。在我的自定义视图中 class 我设置

public void setMyToggleButton1OnCheckedChangedListener(CompoundButton.OnCheckedChangeListener o) {
    myToggleButton1.setOnCheckedChangeListener(o);
}

但是当我尝试在我的主 activity 中处理这个问题时,看起来这个事件从未被触发过。主要是 activity 我试过了

myCustomView.setMyToggleButton1OnCheckedChangedListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                //TODO something here
            }
            else {
                //TODO something here
            }
        }
    });

我确实用常规按钮尝试了我的其他自定义视图,并使用相同的方法将标准按钮单击事件传递给它。我不明白将标准按钮单击事件传递给自定义视图和将切换按钮 checkedchange 事件侦听器传递给自定义视图有什么不同。

PS:这是我对常规按钮及其 OnClickListener 所做的。这个按我的预期工作,这就是为什么我尝试将它复制到 Toggle Button 和 OnCheckedChangeListener。

在 myCustomView2 中(自定义视图包含常规按钮)

public class myCustomView2 extends ConstraintLayout{
    private Button myRegBtn1;
    ...
    public myCustomView2(Context context) {
        this(context, null);
    }

    public myCustomView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
         setAttributes(context, attrs);
    }

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

    private void init(Context context) {
        myRegBtn = findViewById(R.id.btn1);
        ...
    }

    private void setAttributes(Context context, AttributeSet attrs) {
        ...
    }

    public void setMyRegBtnOnClickListener(OnClickListener o) {
        myRegBtn.setOnClickListener(o);
    }
}

在我的 mainActivity 中,

public class myMainActivity extends AppCompatActivity{
    private MyCustomView2 myCustomView2;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        ...
        myCustomView2.setMyRegBtnOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                //TODO: some code execute here when button clicked
            }
        });
    ...
}

您需要自己调用 CheckedChangeListener

即lister.onCheckedChange(已检查)

只有当您扩展 CheckBox 视图时,它才会开箱即用。

0

2020/02/17更新 好的,我找到解决办法了。自定义侦听器在自定义 class 中停止工作的原因是 XML 文件布局。过去,我所有的成功经验都是建立在LinearLayout之上的。我在最初的问题描述中遗漏了这一点。新的自定义视图 XML 文件我使用了 ConstraintLayout,而不是 LinearLayout。这会导致用于 LinearLayout 的 'abnormal' 膨胀行为。

在LinearLayout自定义视图class中,我使用了init()函数

inflate(getContext(), R.layout.my_ll_custom_view,this);

这不适用于约束布局。我必须使用以下代码,

View rootView = LayoutInflater.from(context).inflate(R.layout.my_cl_custom_view, this, false);
ConstraintSet constraintSet = new ConstraintSet();
this.addView(rootView);
ConstraintLayout.LayoutParams clp = new 
ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
ViewGroup.LayoutParams.WRAP_CONTENT);
rootView.setLayoutParams(clp);
constraintSet.clone(this);
constraintSet.connect(rootView.getId(), ConstraintSet.LEFT, this.getId(), ConstraintSet.LEFT, 0);
constraintSet.connect(rootView.getId(), ConstraintSet.RIGHT, this.getId(), ConstraintSet.RIGHT, 0);
constraintSet.connect(rootView.getId(), ConstraintSet.TOP, this.getId(), ConstraintSet.TOP, 0);
constraintSet.connect(rootView.getId(), ConstraintSet.BOTTOM, this.getId(), ConstraintSet.BOTTOM, 0);

可以在https://jordan-dixon.com/2017/12/30/custom-views-with-constraint-layout-and-kotlin/

中找到更多详细信息

我读了这个博客来弄清楚这个问题。我很感激。