How to pass a boolean value between activities with intents
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | public class StartActivity extends AppCompatActivity { private boolean addition=true; private boolean subtraction=false; private boolean multiplication=false; private boolean division=false; public static final String ADDITION =""; public static final String SUBTRACTION =""; public static final String MULTIPLICATION=""; public static final String DIVISION =""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); final Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar); CheckBox additionCheckBox = (CheckBox)findViewById(R.id.additionCheckBox); CheckBox multiplicationCheckBox = (CheckBox)findViewById(R.id.multiplicationCheckBox); CheckBox divisionCheckBox = (CheckBox)findViewById(R.id.divisionCheckBox); CheckBox subtractionCheckBox = (CheckBox)findViewById(R.id.subtractionCheckBox); Button playButton = (Button)findViewById(R.id.playButton); additionCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.isChecked()){ addition = true; }else{ addition = false; } } }); multiplicationCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.isChecked()){ multiplication = true; }else{ multiplication= false; } } }); subtractionCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.isChecked()){ subtraction = true; }else{ subtraction = false; } } }); divisionCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.isChecked()){ division = true; }else{ division = false; } } }); playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(!addition && !subtraction && !multiplication && !division){ final Dialog dialog = new Dialog(StartActivity.this); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("My custom dialog"); //set the custom dialog components - text, image and button TextView text = (TextView) dialog.findViewById(R.id.textView); text.setText("Android custom dialog example!"); Button button = (Button) dialog.findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { dialog.dismiss(); } }); dialog.show(); } else{ Intent i = new Intent(StartActivity.this, Main2Activity.class); i.putExtra(ADDITION,addition); i.putExtra(SUBTRACTION,subtraction); i.putExtra(MULTIPLICATION,multiplication); i.putExtra(DIVISION,division); startActivity(i); } } }); } |
这是开始活动,我试图向每个基本数学运算发送4个布尔变量,以查看用户检查了哪一个。 但是他们在下一次活动中总是假的,我不知道为什么。 这是下一个活动:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); final Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar); setSupportActionBar(myToolbar); Intent i = getIntent(); Bundle b = i.getExtras(); boolean addition = b.getBoolean("ADDITION"); boolean subtraction = Boolean.getBoolean("SUBTRACTION"); boolean multiplication = b.getBoolean("MULTIPLICATION"); boolean division = b.getBoolean("DIVISION"); int operatorCounter =0; TextView additionTextView = (TextView) findViewById(R.id.additionTextView); TextView subtractionTextView = (TextView) findViewById(R.id.subtractionTextView); TextView multiplicationTextView = (TextView) findViewById(R.id.multiplicationTextView); TextView divisionTextView = (TextView) findViewById(R.id.divisionTextView); if (addition) { additionTextView.setText("addition:checked"); }else{ additionTextView.setText("addition:not checked"); } if (subtraction) { subtractionTextView.setText("subtraction:checked"); }else{ subtractionTextView.setText("subtraction:not checked"); } if (multiplication) { multiplicationTextView.setText("multiplication:checked"); }else{ multiplicationTextView.setText("multiplication:not checked"); } if (division) { divisionTextView.setText("division:checked"); }else{ divisionTextView.setText("division:not checked"); } } } |
我在文本框中设置文本以查看值是否正确传递但始终为false。 对不起代码不好我在android中很新,实际上是android及其学校作业中的第四个迷你项目。
我看到的问题是id:
1 2 3 4 |
这应该是一些不同的东西,如:
1 2 3 4 |
并使用相同的值来检索其他活动中的值
Intent intt = new Intent(this,YourAcivity.class);
1 2 3 4 5 6 7 8 9 | intt.putExtra("BooleanKey",true); startActivity(intt); protected void onCreate(Bundle savedInstanceState) { Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName"); |
}
我希望代码可以帮到你
Intent intent = new Intent(this,NextActivity.class);
intent.putExtra("yourBoolName",true);
1 2 | protected void onCreate(Bundle savedInstanceState) { Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName"); |
}
我将使用这篇文章来说明为什么mcve是有用的,因为这是一个完美的例子,我将使用他的代码,清理无用的部分,我们将很快找到问题,我不配或要求upvote但不要如果您反对这个答案,请注意,如果需要,我会删除它。
所以,让我们清理代码,我只对这里的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class StartActivity extends AppCompatActivity { private boolean addition=true; public static final String ADDITION =""; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_start); Button playButton = (Button)findViewById(R.id.playButton); playButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i = new Intent(StartActivity.this, Main2Activity.class); i.putExtra(ADDITION,addition); startActivity(i); } }); |
嗯,这样更好。适合一个屏幕,我们直接看到问题。
恢复相同:
1 2 3 4 5 6 7 8 9 10 11 12 | public class Main2Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent i = getIntent(); Bundle b = i.getExtras(); boolean addition = b.getBoolean("ADDITION"); } } |
嗯,可以在这里使用StartActivity.ADDITION ......
1 2 3 | Intent intent = new Intent(this, NextActivity.class); intent.putExtra("BoolName", true); startActivity(intent ); |
从intent中检索布尔值:
1 |
确保使用相同的键来放置和获取数据。
还要更新
1 |
至
1 | boolean subtraction = b.getBoolean("SUBTRACTION"); |
因为你没有在全局变量中设置值,所以你需要像这样设置第一个全局变量值
1 2 3 4 |
然后设置它
1 2 3 4 5 6 | Intent i = new Intent(StartActivity.this, Main2Activity.class); i.putExtra(ADDITION,addition); i.putExtra(SUBTRACTION,subtraction); i.putExtra(MULTIPLICATION,multiplication); i.putExtra(DIVISION,division); startActivity(i); |
而不是像这样的另一个活动的aceess值
1 2 3 4 | boolean addition = getIntent().getExtras().getBoolean("ADDITION"); boolean sub = getIntent().getExtras().getBoolean("SUBTRACTION"); boolean mult = getIntent().getExtras().getBoolean("MULTIPLICATION"); boolean division = getIntent().getExtras().getBoolean("DIVISION"); |
尝试使用此代码发送和获取值;
传递给第一个活动:
1 2 3 | Intent i = new Intent(getBaseContext(), NameOfActivity.class); i.putExtra("my_boolean_key", myBooleanVariable); startActivity(i); |
检索第二个活动:
1 2 | Bundle bundle = getIntent().getExtras(); boolean myBooleanVariable = bundle.getBoolean("my_boolean_key"); |
希望这可以帮助你