未知属性 onItemSelected/onCheckedChanged [英] Unknown attribute onItemSelected/onCheckedChanged

查看:80
本文介绍了未知属性 onItemSelected/onCheckedChanged的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了 SwitchCompat 并添加了 android:onCheckedChanged.一切正常,但 Android Studio 将此属性标记为警告说明.

I created SwitchCompat and added android:onCheckedChanged. everything works fine, but Android Studio marks this attribute as warning with description.

未知属性 android:onCheckedChanged.

Unknown attribute android:onCheckedChanged.

同样的情况是 AppCompatSpinner 和 android:onItemSelected.

The same case is with AppCompatSpinner with android:onItemSelected.

为什么 Android Studio 会显示此警告?我的 Android Studio 版本是 3.5.1

Why Android Studio displaying this warning? My Android Studio version is 3.5.1

带有警告的截图

推荐答案

那是因为 SwitchCompat 实际上没有这样的属性(参见 documentation),因此您无法在 XML 中分配侦听器.尝试改为实现 onClickListener 或在 XML 之外分配 OnCheckedChanged 侦听器,例如:

That is because SwitchCompat actually does not have such attribute (see documentation), so you can't assign the listener in the XML. Try implementing an onClickListener instead or assign the OnCheckedChanged listener outside of XML, such as:

  final SwitchCompat switchButton = (SwitchCompat) view.findViewById(R.id.switch_button);
  switchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      //do stuff
    }
  });

也可以通过调用binding.switch_button直接返回SwitchButton,从java代码中的dataBinding对象中获取切换按钮.

You can also get the switch button from the dataBinding object in java code by calling binding.switch_button which returns the SwitchButton directly.

这篇关于未知属性 onItemSelected/onCheckedChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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