如何在工具栏菜单中添加日夜切换器

huangapple go评论71阅读模式
英文:

How to add day night switcher in toolbar menu

问题

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.toolbar_menu, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        case R.id.nav_previous:
            onBackPressed();
            break;

        case R.id.nav_next:
            if (webView.canGoForward()) {
                webView.goForward();
            }
            break;

        case R.id.nav_reload:
            checkConnection();
            break;

        // Add functionality for night mode and day mode icons
        case R.id.nav_night_mode:
            // Change app theme to night mode
            break;

        case R.id.nav_day_mode:
            // Change app theme to day mode
            break;
    }

    return super.onOptionsItemSelected(item);
}

In the code snippet above, I've added placeholders for the functionality related to the night mode and day mode icons. You will need to implement the actual logic to change the app theme to night mode and day mode within the respective cases.

英文:

Toolbar menu image

This is the image of my toolbar Menu there are 5 icons i.e backward, forward, reload, moon symbol, and sun symbol.
now I want to add functionality to moon and sun icon i.e when the moon icon is clicked app theme changes to night mode and when sun icon is clicked the app theme. changes to day mode.

*This is the code i have written for backward, forward and reload but now what should. i write for night mode and daymode for android app

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.toolbar_menu,menu);

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {

        case R.id.nav_previous:
            onBackPressed();
            break;


        case R.id.nav_next:
            if (webView.canGoForward()) {
                webView.goForward();
            }
            break;


        case R.id.nav_reload:
            checkConnecttion();
            break;
    }

    return super.onOptionsItemSelected(item);

}

答案1

得分: 1

像这样做

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
        // 获取应用的夜间模式状态
        int nightMode = AppCompatDelegate.getDefaultNightMode();
        case R.id.nav_previous:
            onBackPressed();
            break;

        case R.id.nav_next:
            if (webView.canGoForward()) {
                webView.goForward();
            }
            break;

        case R.id.nav_reload:
            checkConnecttion();
            break;
        
        case R.id.moon:
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            break;

        case R.id.sun:
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            break;
    }

    // 重新创建活动以使主题更改生效。
    recreate();

    return super.onOptionsItemSelected(item);

    }

我建议你查看这个链接:LINK

它很好地描述了解决问题的另一种方法,当然你可以根据需要进行修改。

英文:

do something like this:

@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
    //Get the night mode state of the app
    int nightMode = AppCompatDelegate.getDefaultNightMode();
    case R.id.nav_previous:
        onBackPressed();
        break;


    case R.id.nav_next:
        if (webView.canGoForward()) {
            webView.goForward();
        }
        break;


    case R.id.nav_reload:
        checkConnecttion();
        break;
    
    case R.id.moon:
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
        break;

    case R.id.sun:
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
        break;
}

// Recreate the activity for the theme change to take effect.
recreate();

return super.onOptionsItemSelected(item);

}

I suggest you to take a look at this link: LINK

It describe very well a different way to solve your problem but of course you can modify it according to your needs

huangapple
  • 本文由 发表于 2020年8月26日 02:15:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63584867.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定