📜  如何使用RadioButtons在Android中切换主题?

📅  最后修改于: 2021-05-09 18:38:29             🧑  作者: Mango

我们已经看到android应用带有两种不同类型的模式或主题,分别是暗模式或亮模式,或者我们也将它们称为夜间和早晨模式。在本文中,我们将使用RadioButton在android中实现明暗模式。

我们将在本文中构建什么?

我们将构建一个简单的应用程序,其中将显示简单的文本和RadioButton,其中将使用简单的RadioButtons将主题从浅色切换为深色。下面提供了一个示例视频,以使您对本文中的工作有个大概的了解。注意,我们将使用Java语言实现该项目。

分步实施

步骤1:创建一个新项目

要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Java作为编程语言。

步骤2:使用activity_main.xml文件

导航到应用程序> res>布局> activity_main.xml,然后将以下代码添加到该文件中。以下是activity_main.xml文件的代码。

XML


  
    
    
  
    
    
  
    
    
  
        
        
  
        
        
  
    
      


XML


    #0F9D58
    #0F9D58
    #0F9D58
    #0F9D58
    #FF018786
    #FF000000
    #FFFFFFFF


XML

    
    


XML

    
    


Java
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
  
public class MainActivity extends AppCompatActivity {
    // initializing variables for
    // our radio group and text view.
    private RadioGroup radioGroup;
    private TextView themeTV;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing all our variables.
        radioGroup = findViewById(R.id.idRGgroup);
        themeTV = findViewById(R.id.idtvTheme);
          
        // on below line we are setting on check change method for our radio group.
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // on radio button check change
                switch (checkedId) {
                    case R.id.idRBLight:
                        // on below line we are checking the radio button with id.
                        // on below line we are setting the text to text view as light mode.
                        themeTV.setText("Light Theme");
                        // on below line we are changing the theme to light mode.
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                        break;
                    case R.id.idRBDark:
                        // this method is called when dark radio button is selected
                        // on below line we are setting dark theme text to our text view.
                        themeTV.setText("Dark Theme");
                        // on below line we are changing the theme to dark mode.
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                        break;
                }
            }
        });
    }
}


步骤3:使用colors.xml文件

导航至应用程序> res>值> colors.xml,然后将以下代码添加到其中。

XML格式



    #0F9D58
    #0F9D58
    #0F9D58
    #0F9D58
    #FF018786
    #FF000000
    #FFFFFFFF

步骤4:使用themes.xml进行灯光模式

导航到应用程序> res>值>主题> themes.xml以使用灯光模式,然后将以下代码添加到其中。

XML格式


    
    

第5步:在黑暗模式下使用themes.xml(夜间)。

导航至应用程序> res>值>主题> themes.xml(夜间)以使用黑暗模式,然后将以下代码添加到其中。

XML格式


    
    

步骤6:使用MainActivity。 Java文件

转到MainActivity。 Java文件并参考以下代码。下面是MainActivity的代码。 Java文件。在代码内部添加了注释,以更详细地了解代码。

Java

import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.TextView;
  
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDelegate;
  
public class MainActivity extends AppCompatActivity {
    // initializing variables for
    // our radio group and text view.
    private RadioGroup radioGroup;
    private TextView themeTV;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // initializing all our variables.
        radioGroup = findViewById(R.id.idRGgroup);
        themeTV = findViewById(R.id.idtvTheme);
          
        // on below line we are setting on check change method for our radio group.
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // on radio button check change
                switch (checkedId) {
                    case R.id.idRBLight:
                        // on below line we are checking the radio button with id.
                        // on below line we are setting the text to text view as light mode.
                        themeTV.setText("Light Theme");
                        // on below line we are changing the theme to light mode.
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                        break;
                    case R.id.idRBDark:
                        // this method is called when dark radio button is selected
                        // on below line we are setting dark theme text to our text view.
                        themeTV.setText("Dark Theme");
                        // on below line we are changing the theme to dark mode.
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                        break;
                }
            }
        });
    }
}

现在运行您的应用程序,并查看该应用程序的输出。

输出: