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

查看:350
本文介绍了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天全站免登陆