Android TextView显示相同的文本,同时从不同的活动获取意图

Android TextView显示相同的文本,同时从不同的活动获取意图,android,Android,我在从其他活动中检索意图时遇到问题。因此,我希望应用程序在用户选中复选框并单击按钮后执行。不同活动中的文本视图将显示特定文本。 我让textView显示文本,只是无论单击哪个复选框,它都显示相同的文本 以下是第一项活动: public static final String systemDisplay1 = "checkBoxA"; public String valuetoPass1 = ""; public static final String systemDisplay2 = "chec

我在从其他活动中检索意图时遇到问题。因此,我希望应用程序在用户选中复选框并单击按钮后执行。不同活动中的文本视图将显示特定文本。 我让textView显示文本,只是无论单击哪个复选框,它都显示相同的文本

以下是第一项活动:

public static final String systemDisplay1 = "checkBoxA";
public String valuetoPass1 = "";
public static final String systemDisplay2 = "checkBoxB";
String valuetoPass2 = ""; 
button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent start = new Intent(Main2Activity.this, 
Main4Activity.class);
            start.putExtra(systemDisplay1, 
valuetoPass1);
            start.putExtra(systemDisplay2, 
valuetoPass2);
            startActivity(start);
        }
    });

 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton checkBoxA = (RadioButton) group.findViewById(checkedId);
            if (null != checkBoxA) {
            valuetoPass1= "";
            } else {
                valuetoPass = "Business";
            }
            RadioButton checkBoxB = (RadioButton) group.findViewById(checkedId);
            if (checkBoxB != null) {
                valuetoPass2 = "";
            }else {
                valuetoPass2 = "Education";
            }

        }
    });
}
下面是下一个活动

 TextView systemView = (TextView) findViewById(R.id.systemView);

 Intent intent = getIntent();
    if (intent.getStringExtra(systemdisplay1)!= null){
     systemView.setText("");
    }else {
        systemView.setText("Business");
    }


        Intent intent1 = getIntent();
        if (intent1.getStringExtra(systemDisplay2)!= null){
            systemView.setText("");
        }else{
            systemView.setText("Education");
        }
    }
}

无论我点击哪个复选框,文本视图都会显示教育。我能做些什么来解决这个问题?

我不确定您是否希望允许多重选择,例如允许选择教育和商业。如果是这样的话: 使用三个布尔变量来存储每个决策,并将它们放在意图上。 如果只允许一个选择:
将复选框更改为带有单选按钮的单选组,并仅使用一个字符串来存储选择。

也许您不必在顶部声明数据

public static final String systemDisplay1 = "checkBoxA";
public String valuetoPass1 = "";
public static final String systemDisplay2 = "checkBoxB";
String valuetoPass2 = "";
String valuetoPass3 = ""; 
button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
        Intent start = new Intent(Main2Activity.this, 
Main4Activity.class);
            start.putExtra(systemDisplay1, 
valuetoPass1);
            start.putExtra(systemDisplay2, 
valuetoPass2);
            startActivity(start);
        }
    });

checkBoxA.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton 
compoundButton, boolean b) {
            if (checkBoxA.isChecked()) {
                valuetoPass1 = "Business";
            }
        }
    });
checkBoxB.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton 
compoundButton, boolean b) {
    if (checkBoxB.isChecked()){
        valuetoPass2 = "Education";
    }
}
});
checkBoxC.setOnCheckedChangeListener(new 
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton 
compoundButton, boolean b) {
    if (checkBoxC.isChecked()){
        valuetoPass3 = "Engineering";
    }
}
});
}

无论是否选中检查,字符串valuetoPass2始终包含教育,因为您已经声明了它,所以接收活动中的代码使systemView文本视图成为教育

最初将valuetoPass1和valuetoPass2设置为null,然后仅在选中该复选框时尝试设置值


首选方法:

尝试将复选框更改为单选按钮并形成一个组

代码:

接收活动

TextView systemView = (TextView) findViewById(R.id.systemView);
    if (this.getIntent().getStringExtra(Main2Activity.KEY)!= null)
    {
        systemView.setText(this.getIntent().getStringExtra(Main2Activity.KEY);
    } 
    else 
    {
        systemView.setText(" Set any default value");
    }
TextView systemView = (TextView) findViewById(R.id.systemView);
    if (this.getIntent().getStringExtra(Main2Activity.KEY)!= null)
    {
        systemView.setText(this.getIntent().getStringExtra(Main2Activity.KEY);
    } 
    else 
    {
        systemView.setText(" Set any default value");
    }

据我所知,您希望将一个特定的选择发送到另一个活动。因此,如果您使用单选按钮而不是复选框,效果会更好

public static final String systemDisplay1 = "selection";
public String valuetoPass1 = "Business";

根据用户选择的单选按钮将值更改为ASS1。并使用intent将此字符串传递给另一个活动。从intent中获取额外字符串并将其设置为TextView。

首选方法:

尝试将复选框更改为单选按钮并形成一个组

代码:

}

接收活动

TextView systemView = (TextView) findViewById(R.id.systemView);
    if (this.getIntent().getStringExtra(Main2Activity.KEY)!= null)
    {
        systemView.setText(this.getIntent().getStringExtra(Main2Activity.KEY);
    } 
    else 
    {
        systemView.setText(" Set any default value");
    }
TextView systemView = (TextView) findViewById(R.id.systemView);
    if (this.getIntent().getStringExtra(Main2Activity.KEY)!= null)
    {
        systemView.setText(this.getIntent().getStringExtra(Main2Activity.KEY);
    } 
    else 
    {
        systemView.setText(" Set any default value");
    }

我猜你的第二个if没有找到任何额外的,比如说你正在传递第一个if,所以它默认为else分支,它设置教育覆盖第一个文本的设置,就像你要求代码那样。请想一想你应该做什么,这非常简单。下一个活动你将发送什么?共享代码ValueToPass2已初始化为Education,并且仅设置为Education,因此您当前的所有代码都将显示为Education。。。不过,还不清楚你想做什么,因此无法真正提出解决方案。您的复选框编码无效。也许你应该只传递一个值?@Alessio抱歉,我现在有点理解这个问题,但我不知道如何解决它。我该怎么办呢?还有@Mira试试这个:把问题分解成更小的步骤,然后用Log.dmira之类的东西进行调试,我的值是:+myValue——每个步骤的值;这样你就可以确保每一步都符合你的要求如果你选中两个复选框会发生什么?Used RadioButton我试过这个代码,但我仍然有同样的问题。就像我单击第一个单选按钮一样,它会显示如果我单击第二个单选按钮,应该显示的文本。