📜  在 Android 中实现具有自定义布局的 RadioButton

📅  最后修改于: 2022-05-13 01:54:25.708000             🧑  作者: Mango

在 Android 中实现具有自定义布局的 RadioButton

RadioButtons 允许用户从一组选项中选择一个选项。如果您认为用户需要并排查看所有可用选项,则应将 RadioButtons 用于互斥的可选集。在本文中,我们将了解如何在 android 中实现具有自定义布局的 RadioButton。

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

这是我们将在本文中构建的示例视频。请注意,我们将使用Java语言来制作这个项目。

分步实施

第 1 步:创建一个新项目

  • 打开一个新项目。
  • 我们将使用Java语言开发 Empty Activity。保持所有其他选项不变。
  • 在您方便的时候命名应用程序。
  • 将有两个名为 activity_main.xml 和 MainActivity 的默认文件。Java

如果您不知道如何在 Android Studio 中创建新项目,可以参考如何在 Android Studio 中创建/启动新项目?

步骤 2. 添加所需的依赖项

导航到 settings.gradle 文件并在其中使用以下代码

maven {url 'https://jitpack.io'}

导航到 build.gradle(module) 文件并添加以下依赖项

implementation 'com.github.worker8:radiogroupplus:1.0.1'

步骤 3. 处理 XML 文件

导航到 app > res > layout > activity_main.xml 并将以下代码添加到该文件。下面是 activity_main.xml 文件的代码。

XML


  
    
        
        
  
            
            
            
            
            
              
        
        
        
  
            
            
            
            
            
  
        
        
          
  
            
            
            
            
            
  
        
        
        
  
            
    
            
    
            
  
        
  
        
  
            


Java
package com.example.radiobuttoncustomlayout;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
import worker8.com.github.radiogroupplus.RadioGroupPlus;
  
public class MainActivity extends AppCompatActivity {
  
    // Initialize variable
    RadioGroupPlus radioGroupPlus;
    Button btReset,btSubmit;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Assign variable
        radioGroupPlus=findViewById(R.id.radio_group);
        btReset=findViewById(R.id.bt_reset);
        btSubmit=findViewById(R.id.bt_submit);
  
        btReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Clear check
                radioGroupPlus.clearCheck();
            }
        });
  
        btSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // get started radio button id
                int id=radioGroupPlus.getCheckedRadioButtonId();
                // Check condition
                switch (id)
                {
                    case R.id.radio_button1:
                        // When id is equal to button
                        // initialize string
                        String s="One";
                        // Display toast
                        displayToast(s);
                        break;
                    case R.id.radio_button2:
                        // When id is equal to button
                        // Display toast
                        displayToast("Two");
                        break;
                     case R.id.radio_button3:
                        // When id is equal to button
                        // Display toast
                        displayToast("Three");
                        break;
                     case R.id.radio_button4:
                        // When id is equal to button
                        // Display toast
                        displayToast("Four");
                        break;
                }
            }
        });
  
        radioGroupPlus.setOnCheckedChangeListener(new RadioGroupPlus.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroupPlus group, int checkedId) {
                // When check the radio button
                // Display toast
                displayToast("Selected Id " + checkedId);
            }
        });
    }
  
    private void displayToast(String s) {
        // Initialize toast
        Toast.makeText(getApplicationContext()
        ,s,Toast.LENGTH_SHORT).show();
    }
}


步骤 4. 处理Java文件

导航到 MainActivity。 Java文件并在其中使用以下代码。代码中添加了注释以便更好地理解。

Java

package com.example.radiobuttoncustomlayout;
  
import androidx.appcompat.app.AppCompatActivity;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
  
import worker8.com.github.radiogroupplus.RadioGroupPlus;
  
public class MainActivity extends AppCompatActivity {
  
    // Initialize variable
    RadioGroupPlus radioGroupPlus;
    Button btReset,btSubmit;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        // Assign variable
        radioGroupPlus=findViewById(R.id.radio_group);
        btReset=findViewById(R.id.bt_reset);
        btSubmit=findViewById(R.id.bt_submit);
  
        btReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Clear check
                radioGroupPlus.clearCheck();
            }
        });
  
        btSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // get started radio button id
                int id=radioGroupPlus.getCheckedRadioButtonId();
                // Check condition
                switch (id)
                {
                    case R.id.radio_button1:
                        // When id is equal to button
                        // initialize string
                        String s="One";
                        // Display toast
                        displayToast(s);
                        break;
                    case R.id.radio_button2:
                        // When id is equal to button
                        // Display toast
                        displayToast("Two");
                        break;
                     case R.id.radio_button3:
                        // When id is equal to button
                        // Display toast
                        displayToast("Three");
                        break;
                     case R.id.radio_button4:
                        // When id is equal to button
                        // Display toast
                        displayToast("Four");
                        break;
                }
            }
        });
  
        radioGroupPlus.setOnCheckedChangeListener(new RadioGroupPlus.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroupPlus group, int checkedId) {
                // When check the radio button
                // Display toast
                displayToast("Selected Id " + checkedId);
            }
        });
    }
  
    private void displayToast(String s) {
        // Initialize toast
        Toast.makeText(getApplicationContext()
        ,s,Toast.LENGTH_SHORT).show();
    }
}

这是我们应用程序的最终输出。

输出: