gpt4 book ai didi

Android:在开关上使用 setOnCheckedChangeListener 时,我得到 'Cannot Resolve Symbol Error'

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:52:57 26 4
gpt4 key购买 nike

package com.example.koustav.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;

public class MainActivity extends Activity
{

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

Switch swi = (Switch) findViewById(R.id.swch);
swi.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) //Line A
{

}
});

public void onClick(View view)
{
boolean isOn = ((Switch) view).isChecked();

if (isOn)
{
((TextView) findViewById(R.id.largey)).setTextColor(getResources().getColor(R.color.black));
}
else
{
((TextView) findViewById(R.id.largey)).setTextColor(getResources().getColor(R.color.white));
}

}

}

我正在为这个项目使用 Android Studio。我已经在互联网上广泛搜索解决方案,但找不到。

我一直在尝试对 Switch 对象使用 setOnCheckedChangeListener。但是,无论我尝试什么,我总是会收到 Cannot Resolve Symbol 'setOnCheckedChangeListener' 错误。此外,在 A 行,对象 buttonView 带有红色下划线,给出相同的错误 Cannot Resolve Symbol 'buttonView'

我已经导入了必要的(并推荐作为其他人的答案)类。我正在使用适用于 android 4.0 及更高版本的 API。我基本上希望通过监听器重新实现 onClick 方法,因为监听器适用于点击和滑动,而 onClick 仅适用于点击而不适用于滑动。

最佳答案

你必须在 onCreate() 中初始化 Switch

关于Android:在开关上使用 setOnCheckedChangeListener 时,我得到 'Cannot Resolve Symbol Error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29844990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com