Android:在开关上使用 setOnCheckedChangeListener 时,我收到“无法解决符号错误”

2024-02-18

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。我在互联网上搜索了可扩展的解决方案,但找不到。

我一直在尝试使用setOnCheckedChangeListener for a Switch目的。然而,无论我尝试什么,我总是得到一个无法解析符号“setOnCheckedChangeListener”错误。另外,在 A 行,对象 buttonView 带有红色下划线,给出了相同的错误无法解析符号“buttonView”.

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


你必须初始化Switch inside onCreate()

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:在开关上使用 setOnCheckedChangeListener 时,我收到“无法解决符号错误” 的相关文章

随机推荐