我在 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/