java - Android 复选框选中一个变量

标签 java android

我有很多复选框,如果像这样检查,我将其保存在字符串中:

 String juice = "";

 if (apple.isChecked()) {
      juice = apple.getText().toString().trim() + " " + etiquetas;
 }
 if (orange.isChecked()) {
      juice = orange.getText().toString().trim() + " " + etiquetas;
 }

但是当我需要在另一个 Activity 中选中该复选框时,我不知道如何检查,因为我保存在一个 Activity 中。 我确实需要在一个变量中像这样,因为它们是考试说明。这是我的另一项 Activity 的代码;

Intent intent = getIntent();
if(intent != null){
     String juice_2 = (String) getIntent().getStringExtra("EXTRA_JUICE"); 
     //apple.setChecked();
}

最佳答案

在当前 Activity 中,您可以使用单个字符串,其中包含多个由空格字符分隔的 juice 单词。

在目标 Activity 中,根据空格字符将字符串拆分为字符串数组,然后设置 checkBox 值(如果该数组包含您的特定果汁)。

在 Activity A:

String juice = "";

if (apple.isChecked()) {
    juice = apple.getText().toString().trim() + " ";
}
if (orange.isChecked()) {
    juice = orange.getText().toString().trim() + " ";
}

// Going to Activity B
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("JUICE", juice);
startActivity(intent);

在 Activity B:

if (getIntent() != null) {
    String juice = getIntent().getStringExtra("JUICE");
    if (juice != null) {
        String[] splittedJuice = juice.split(" ");
        ArrayList list = new ArrayList();
        // Convert array into a List
        Collections.addAll(list, oldLines);
        // Check if the list contains "orange"
        if (list1.contains("orange")) {
            orange.setChecked(true);
        }

        if (list1.contains("apple")) {
            apple.setChecked(true);
        }

    }
}

关于java - Android 复选框选中一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61394562/

相关文章:

android - 如何在 Redmi 上使用应用程序图标显示徽章数量?

android - 如何将按钮添加到自定义滚动和缩放 SurfaceView?

java - 如何将包图标添加到数组列表中

java - 如何多次实现相同的接口(interface),但使用不同的泛型?

java - 使用 java JNDI 更新信息 Activity 目录数据 ?

java - PNG 编码器 - 添加自己的过滤器实现

android - 如何在 Android 中每 5 分钟刷新一次文件读取?

java - Java中父子线程的通信

java - 使用 Spring Boot 执行器安全关闭休息服务器?

android - 在使用 Button 调试 NullPointerException 时需要帮助