无法应用CompoundButton中的setOnCheckedChangeListener

时间:2017-04-01 18:39:07

标签: java android

我尝试添加onCheckedChangeListener Android Studio this上显示错误。我想我做错了什么但我真的不知道在哪里。

AS showing me this error but I can't find solve (image)

代码:

import ...

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    WebView wv;
    FloatingActionButton fab;
    ProgressBar loading;
    Intent intent;
    Context context;
    Switch mySwitch;
    private int mProgressStatus;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //code
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        setTitle("");
        wv = (WebView) findViewById(R.id.wv);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        FloatingActionButton myFab = (FloatingActionButton) findViewById(R.id.fab);
        myFab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                wv.loadUrl("***");
            }
        });

        final NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setFocusable(true);
        wv.setFocusableInTouchMode(true);
        wv.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        wv.loadUrl("***");
        wv.setWebViewClient(new WebViewClient());

        mySwitch = (Switch)findViewById(R.id.switch1);
        mySwitch.setOnCheckedChangeListener(this);
    }

    public void onCheckedChange(CompoundButton buttonView,boolean isChecked) {
        if(isChecked) {
            WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(true);
        }
        else {
            WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
            wifiManager.setWifiEnabled(false);
        }
    }

    @Override
    public void onBackPressed() {
        if (wv.canGoBack()){
            wv.goBack();
        }
        else {
            super.onBackPressed();
        }
    }

    //Other code...

1 个答案:

答案 0 :(得分:1)

更改为:

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        // perform necessary action when check event happens
    }
});

或者让您的活动实现适当的界面:

public class MainActivity extends AppCompatActivity implements 
        NavigationView.OnNavigationItemSelectedListener, 
        CompoundButton.OnCheckedChangeListener {
...
}