Java 如何将CheckedChange侦听器传递给自定义视图
更新日期:2020/02/17 好的,我找到解决办法了。导致自定义侦听器在自定义类中停止工作的原因是XML文件布局。在过去,我所有的成功经验都是建立在线性布局的基础上的。我在最初的问题描述中没有提到这一点。我使用的新自定义视图XML文件ConstraintLayout,而不是LinearLayout。这会导致用于线性布局的“异常”充气行为 在LinearLayout自定义视图类的init()函数中,我使用了Java 如何将CheckedChange侦听器传递给自定义视图,java,android,android-custom-view,event-listener,Java,Android,Android Custom View,Event Listener,更新日期:2020/02/17 好的,我找到解决办法了。导致自定义侦听器在自定义类中停止工作的原因是XML文件布局。在过去,我所有的成功经验都是建立在线性布局的基础上的。我在最初的问题描述中没有提到这一点。我使用的新自定义视图XML文件ConstraintLayout,而不是LinearLayout。这会导致用于线性布局的“异常”充气行为 在LinearLayout自定义视图类的init()函数中,我使用了 inflate(getContext(), R.layout.my_ll_custom_
inflate(getContext(), R.layout.my_ll_custom_view,this);
inflate(getContext(), R.layout.my_ll_custom_view,this);
这对constraintlayout不起作用。
我必须使用以下代码
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);
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);
有关更多详细信息,请参阅
我读这篇博客就是为了弄明白这一点。我很感激
======最初的问题描述从这里开始======
我用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:下面是我为常规按钮和它的onclick监听器所做的。这一个正如我预期的那样工作,这就是为什么我尝试将它复制到切换按钮和oncheckedchangedlistener
在myCustomView2中(自定义视图包含常规按钮)
在我的主要活动中
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打电话 i、 e列表器的onCheckedChange(isChecked) 仅当您扩展复选框视图时,它才会开箱即用。0 2017年2月20日更新好的,我找到了解决方案。导致自定义侦听器在自定义类中停止工作的原因是XML文件布局。在过去,我所有的成功经验都是建立在线性布局的基础上的。我在最初的问题描述中没有提到这一点。我使用的新自定义视图XML文件ConstraintLayout,而不是LinearLayout。这会导致用于线性布局的“异常”充气行为 在LinearLayout自定义视图类的init()函数中,我使用了
inflate(getContext(), R.layout.my_ll_custom_view,this);
inflate(getContext(), R.layout.my_ll_custom_view,this);
这对constraintlayout不起作用。我必须使用以下代码
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);
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);
有关更多详细信息,请参阅
我读这篇博客就是为了弄明白这一点。非常感谢。请显示myCustomView中的更多代码,您需要在单击视图时调用侦听器,是吗?@LenaBru此处没有更多相关代码。如果您指的是带有常规按钮的自定义视图,比如说它是myCustomView2,那么我所做的就是声明按钮,私有按钮myRegularBtn,从布局分配视图,myRegularBtn=findViewById(R.id.button1),并将OnClickListener连接/传递给它,public void设置MyRegularBTnonClickListener(OnClickListener o){myRegularBtn.setOnClickListener(o);}就是这样。这就是为什么我认为OnCheckedChangeListener会以同样的方式工作。