📜  Android中的自定义复选框

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

Android中的自定义复选框

CheckBox 属于android.widget.CheckBox类。 Android CheckBox 类是 CompoundButton 类的子类。它通常用于用户可以从给定的选项列表中选择一个或多个选项的地方。在本文中,我们将了解如何在 android 中实现自定义 CheckBox。

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

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

分步实施

第 1 步:创建一个新项目

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

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

步骤 2. 使用 activity_main.xml 文件

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

XML


  
    
    
    
    
    
    
    
    
    


Java
package com.example.customcheckbox;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
    CheckBox cbAndroid, cbJava, cbPhp;
    Button btSubmit;
    TextView tvOutput;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        cbAndroid = findViewById(R.id.cb_android);
        cbJava = findViewById(R.id.cb_java);
        cbPhp = findViewById(R.id.cb_php);
        btSubmit = findViewById(R.id.bt_submit);
        tvOutput = findViewById(R.id.output);
  
        cbAndroid.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    if (cbAndroid.isChecked()) {
                        cbAndroid.setTextColor(
                            getResources().getColor(
                                R.color.purple_200));
                    }
                    else {
                        cbAndroid.setTextColor(
                            getResources().getColor(
                                R.color.black));
                    }
                }
            });
        cbJava.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    if (cbJava.isChecked()) {
                        cbJava.setTextColor(
                            getResources().getColor(
                                R.color.purple_200));
                    }
                    else {
                        cbJava.setTextColor(
                            getResources().getColor(
                                R.color.black));
                    }
                }
            });
        cbPhp.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    if (cbPhp.isChecked()) {
                        cbPhp.setTextColor(
                            getResources().getColor(
                                R.color.purple_200));
                    }
                    else {
                        cbPhp.setTextColor(
                            getResources().getColor(
                                R.color.black));
                    }
                }
            });
  
        btSubmit.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    String s = "";
                    if (cbAndroid.isChecked()) {
                        s += "\n Android";
                    }
                    if (cbJava.isChecked()) {
                        s += "\n Java";
                    }
                    if (cbPhp.isChecked()) {
                        s += "\n Php";
                    }
                    tvOutput.setText(s);
                }
            });
    }
}


第 3 步:使用 MainActivity。Java

转到 MainActivity。 Java文件并参考以下代码。下面是 MainActivity 的代码。 Java文件。

Java

package com.example.customcheckbox;
  
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
  
public class MainActivity extends AppCompatActivity {
    CheckBox cbAndroid, cbJava, cbPhp;
    Button btSubmit;
    TextView tvOutput;
  
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  
        cbAndroid = findViewById(R.id.cb_android);
        cbJava = findViewById(R.id.cb_java);
        cbPhp = findViewById(R.id.cb_php);
        btSubmit = findViewById(R.id.bt_submit);
        tvOutput = findViewById(R.id.output);
  
        cbAndroid.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    if (cbAndroid.isChecked()) {
                        cbAndroid.setTextColor(
                            getResources().getColor(
                                R.color.purple_200));
                    }
                    else {
                        cbAndroid.setTextColor(
                            getResources().getColor(
                                R.color.black));
                    }
                }
            });
        cbJava.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    if (cbJava.isChecked()) {
                        cbJava.setTextColor(
                            getResources().getColor(
                                R.color.purple_200));
                    }
                    else {
                        cbJava.setTextColor(
                            getResources().getColor(
                                R.color.black));
                    }
                }
            });
        cbPhp.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    if (cbPhp.isChecked()) {
                        cbPhp.setTextColor(
                            getResources().getColor(
                                R.color.purple_200));
                    }
                    else {
                        cbPhp.setTextColor(
                            getResources().getColor(
                                R.color.black));
                    }
                }
            });
  
        btSubmit.setOnClickListener(
            new View.OnClickListener() {
                @Override public void onClick(View view)
                {
                    String s = "";
                    if (cbAndroid.isChecked()) {
                        s += "\n Android";
                    }
                    if (cbJava.isChecked()) {
                        s += "\n Java";
                    }
                    if (cbPhp.isChecked()) {
                        s += "\n Php";
                    }
                    tvOutput.setText(s);
                }
            });
    }
}

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

输出: