Android:在开关上使用 setOnCheckedChangeListener 时,我收到“无法解决符号错误" [英] Android: When using setOnCheckedChangeListener on switches I get 'Cannot Resolve Symbol Error'

查看:25
本文介绍了Android:在开关上使用 setOnCheckedChangeListener 时,我收到“无法解决符号错误"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Android Studio for this project. I have searched the interwebs for a solution extensibly but could not find one.

我一直在尝试将 setOnCheckedChangeListener 用于 Switch 对象.但是,无论我尝试什么,我总是收到 无法解析符号 'setOnCheckedChangeListener' 错误.此外,在 A 行,对象 buttonView 带有红色下划线,给出相同的错误无法解析符号 'buttonView'.

I have been trying to use setOnCheckedChangeListener for a Switch object. However no matter what I try, I always get a Cannot Resolve Symbol 'setOnCheckedChangeListener' error. Also, at the line A, the object buttonView is underlined red giving the same error Cannot Resolve Symbol 'buttonView'.

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

I have imported the necessary (and recommended as answers for other people) classes. I am working with APIs for android 4.0 and above. I basically want the onClick method to be re-implemented via the listener because the listener works with both clicks and swipes whereas onClick only works with clicks and not swipes.

推荐答案

你必须在onCreate()

这篇关于Android:在开关上使用 setOnCheckedChangeListener 时,我收到“无法解决符号错误"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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