gpt4 book ai didi

java - 无法应用CompoundButton 中的setOnCheckedChangeListener

转载 作者:行者123 更新时间:2023-12-02 13:31:47 36 4
gpt4 key购买 nike

我尝试添加 onCheckedChangeListenerAndroid Studiothis 上显示错误。我觉得我做错了什么,但我真的不知道错在哪里。

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...

最佳答案

更改为:

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

或者让您的 Activity 实现适当的接口(interface):

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

关于java - 无法应用CompoundButton 中的setOnCheckedChangeListener,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43160618/

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