android - 尝试将多个复选框添加到 Android Studio 上的数组中

标签 android checkbox

我在 Android Studio 上做一个应用程序,我试图将我所有的复选框 ID 添加到一个数组中,这样我就可以使用它,而无需手动执行。 我试着用另一种方式来做,但我在谷歌上找不到任何帮助我的东西,

所以这是我的目标:

我想获取我所有的复选框 ID,以便获取它们的文本。而且我不想手动操作,因为我有很多复选框。

我尝试自己编写代码,但收到消息错误。 这是我的代码:

 CheckBox[] MinhaCheckBox;
    SharedPreferences Dados;
   String MinhaPasta = "Pasta";
    String valor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void btn_Gerar(View v)
    {
        Dados = getSharedPreferences(MinhaPasta,0);
        SharedPreferences.Editor Edita = Dados.edit();
        int IDTexBoz[] = {R.id.checkBox,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,
                R.id.checkBox6,R.id.checkBox7,R.id.checkBox8};
        for(int i = 0 ; i < IDTexBoz.length; i++)
        {


 *//I GOT AN ERRO HERE. please help me.*
 * // when i try to put mycheckbox on my array, i got th error!*
            MinhaCheckBox[i] = (CheckBox)findViewById(IDTexBoz[i]);

        }
        for (int a= 0; a < MinhaCheckBox.length;a++)
        {
            if(MinhaCheckBox[a].isChecked())
            valor += MinhaCheckBox[a].getText().toString() + ";";
        }
        Edita.putString("Dado", valor);
        Edita.commit();

        Intent MeuIntent = new Intent(this,Main2Activity.class);
        startActivity(MeuIntent);
    }
}

最佳答案

您需要初始化复选框数组。

关于android - 尝试将多个复选框添加到 Android Studio 上的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33353113/

相关文章:

java - Android 服务器应用程序 : SSLHandshakeException (unable to find valid certification path to requested target)

Android Google Maps V2 - OnInfoWindowClick on several markers

checkbox - 获取转发器中所有复选框的值

android - 在适配器中设置时未选中复选框

javascript - 在 Bootstrap 复选框中启用多选选项?

Android studio - 构建后无法在设备上找到我的应用程序

android - 不允许在底部应用栏中设置标题

android - 如何告诉 ORMLite 不要用 null 覆盖数据库中的值

javascript - jquery 复选框 :checked is not working

Android - 当我们选择复选框时,Spinner 需要改变