如何将检查更改侦听器传递到自定义视图



我用 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所做的。这个工作如我预期的那样,这就是为什么我尝试将其复制到切换按钮和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);
}
}

在我的主活动中,

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(isChecked(

仅当您扩展复选框视图时,它才会开箱即用。

好的,我找到了解决方案。导致自定义侦听器在自定义类中停止工作的原因是 XML 文件布局。过去,我所有的成功经验都是建立在LinearLayout之上的。我在最初的问题描述中错过了这一点。新的自定义视图XML文件我使用了ConstraintLayout,而不是LinearLayout。这会导致用于与 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/中找到

我读了这个博客来弄清楚这一点。我很感激。

最新更新