我在从其他活动中检索Intent时遇到问题。因此,我希望应用程序执行的操作是在用户选中复选框并单击一个按钮之后。不同活动中的textView
将显示某些文本。
不管单击哪个textView
,checkBox
都将显示相同的文本。
这是第一个活动:
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");
}
}
}
无论我单击哪个checkBox
,TextView
都将显示“教育”。我该怎么办才能解决这个问题?
答案 0 :(得分:2)
据我了解,您想将一个特定选择发送到另一项活动。因此,如果您使用单选按钮而不是复选框,那就更好了。
public static final String systemDisplay1 = "selection";
public String valuetoPass1 = "Business";
根据用户选择的单选按钮将值更改为Pass1。并使用意图将此字符串传递给另一个活动。从意图中获取额外的字符串,并将其设置为TextView。
答案 1 :(得分:1)
我不确定是否要允许多项选择,例如允许选择教育和商业。如果是这样的话: 使用三个布尔变量存储每个决策,并将其放入意图中。 如果您只允许一个选择: 将复选框更改为带有单选按钮的单选组,并仅使用一个字符串存储选择。
答案 2 :(得分:0)
首选方法:
尝试将复选框更改为“单选”按钮,并形成一个RadioGroup。
代码:
public static final String systemDisplay1 = "checkBoxA";
public String valuetoPass1 = "Business";
public static final String systemDisplay2 = "checkBoxB";
String valuetoPass2 = "Education";
String intentParam = null; // set default value
final static String KEY = "paramKey";
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent start = new Intent(Main2Activity.this,
Main4Activity.class);
start.putExtra(KEY,intentParam);
startActivity(start);
}
});
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
if (null != radioButton)
{
intentParam = ""// set the value depending on the type of radio button the user has checked.
}
}
});
}
接收活动
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");
}
答案 3 :(得分:-1)
也许您不能在顶部声明数据
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";
}
}
});
}
答案 4 :(得分:-2)
无论检查与否,字符串valuetoPass2
都包含Education
,因为您已经声明了它,因此接收活动中的代码使systemView
TextView成为{{1 }}。
首先将Education
和valuetoPass1
设置为null,然后仅在选中复选框的情况下尝试设置值。
首选方法:
尝试将复选框更改为“单选”按钮,并形成一个RadioGroup。
代码:
valuetoPass2
接收活动
public static final String systemDisplay1 = "checkBoxA";
public String valuetoPass1 = "Business";
public static final String systemDisplay2 = "checkBoxB";
String valuetoPass2 = "Education";
String intentParam = null; // set default value
final static String KEY = "paramKey";
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent start = new Intent(Main2Activity.this,
Main4Activity.class);
start.putExtra(KEY,intentParam);
startActivity(start);
}
});
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
if (null != radioButton)
{
intentParam = ""// set the value depending on the type of radio button the user has checked.
}
}
});
}