温馨提示:本文翻译自stackoverflow.com,查看原文请点击:java - How to pass a CheckedChange Listener to a custom view
android android-custom-view event-listener java

java - 如何将CheckedChange侦听器传递到自定义视图

发布于 2020-05-03 20:27:27

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

在LinearLayout自定义视图类的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个切换按钮制作了一个自定义视图。在我的自定义视图类中

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

但是,即使在我的主要活动中尝试进行处理时,也似乎从未触发过此事件。在主要活动中,我尝试了

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
            }
        });
    ...
}

查看更多

提问者
apolloneo
被浏览
8
apolloneo 2020-02-17 22:56

0

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

在LinearLayout自定义视图类的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/中找到更多详细信息。

我读了这个博客来弄清楚这一点。我对此表示感谢。