【发布时间】:2020-11-24 12:09:01
【问题描述】:
- 第一节
包 com.gmsofficial.GmSOfficial;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.CompoundButton;
import android.widget.Switch;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;
- 第二节
导入 com.google.android.material.navigation.NavigationView;
public class NavigationDrawer extends AppCompatActivity {
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
if (loadState() == true){
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
setTheme(R.style.darkTheme);
} else {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
setTheme(R.style.Theme_AppCompat);
}
setUpToolbar();
navigationView = (NavigationView) findViewById(R.id.navigation_menu);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
3.第三节
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId())
{
case R.id.nav_home:
Intent intent = new Intent(NavigationDrawer.this, NavigationDrawer.class);
startActivity(intent);
break;
case R.id.nav_Website:
intent = new Intent(NavigationDrawer.this, Websitewebview.class);
startActivity(intent);
break;
case R.id.nav_Mobiles:
intent = new Intent(NavigationDrawer.this, Mobileswebview.class);
startActivity(intent);
break;
-
第四节
case R.id.nav_TechNews: intent = new Intent(NavigationDrawer.this, TechNewswebview.class); startActivity(intent); break; case R.id.nav_Gadgets: intent = new Intent(NavigationDrawer.this, Gadgetswebview.class); startActivity(intent); break; case R.id.nav_CustomROM: intent = new Intent(NavigationDrawer.this, CustomRomwebview.class); startActivity(intent); break; menuItem.setActionView(R.layout.theme_switch); final Switch themeswitch = (Switch) menuItem.getActionView().findViewById(R.id.action_switch); if (loadState() == true){ themeswitch.setChecked(true); } -
无法访问的错误部分
themeswitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if (isChecked) { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); saveState(true); recreate(); } else { AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO); saveState(false); } } }); break; case R.id.nav_AboutUs: intent = new Intent(NavigationDrawer.this, AboutUS.class); startActivity(intent); break; -
第六节 //粘贴您的隐私政策链接
// case R.id.nav_Policy:{ // // Intent browserIntent = new Intent(Intent.ACTION_VIEW , Uri.parse("")); // startActivity(browserIntent); // // } // 休息; 案例 R.id.nav_share:{
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND); sharingIntent.setType("text/plain"); String shareBody = "http://play.google.com/store/apps/detail?id=" + getPackageName(); String shareSub = "Try now"; sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareSub); sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody); startActivity(Intent.createChooser(sharingIntent, "Share using")); } break; } return false; } });}
-
第七节
public void setUpToolbar() { drawerLayout = findViewById(R.id.drawerLayout); Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name); drawerLayout.addDrawerListener(actionBarDrawerToggle); actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.purple)); actionBarDrawerToggle.syncState();}
private void saveState(Boolean state){ SharedPreferences sharedPreferences = getSharedPreferences("GmS Official", MODE_PRIVATE); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putBoolean("NightMode", state); editor.apply();}
private Boolean loadState(){ SharedPreferences sharedPreferences = getSharedPreferences("GmS Official", MODE_PRIVATE); Boolean state = sharedPreferences.getBoolean("NightMode", false); return state;} }
【问题讨论】:
-
请原谅我的错误
-
我们不知道该代码之间的关系。它在一个文件中吗?在一种方法中? ... ?我们也不知道您到底从哪里得到错误
标签: javascript java android