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个布尔变量的下一个活动,以查看用户检查了哪一个。但是他们在下一次活动中总是假的,我不知道为什么。这是下一个活动:
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及其学校作业中的第四个迷你项目。
答案 0 :(得分:4)
我看到的问题是id:
public static final String ADDITION = "";
public static final String SUBTRACTION = "";
public static final String MULTIPLICATION= "";
public static final String DIVISION = "";
这应该是不同的东西,如:
public static final String ADDITION = "ADDITION";
public static final String SUBTRACTION = "SUBTRACTION";
public static final String MULTIPLICATION= "MULTIPLICATION";
public static final String DIVISION = "DIVISION";
并使用相同的值来检索其他活动中的值
答案 1 :(得分:1)
尝试使用此代码发送和获取值;
传递给第一个活动:
Intent i = new Intent(getBaseContext(), NameOfActivity.class);
i.putExtra("my_boolean_key", myBooleanVariable);
startActivity(i);
检索第二个活动:
Bundle bundle = getIntent().getExtras();
boolean myBooleanVariable = bundle.getBoolean("my_boolean_key");
希望这可以帮助你
答案 2 :(得分:1)
因为你没有在全局变量中设置值,所以你需要像这样设置第一个全局变量值
public static final String ADDITION = "ADDITION";
public static final String SUBTRACTION = "SUBTRACTION";
public static final String MULTIPLICATION= "MULTIPLICATION";
public static final String DIVISION = "DIVISION";
然后设置
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值
boolean addition = getIntent().getExtras().getBoolean("ADDITION");
boolean sub = getIntent().getExtras().getBoolean("SUBTRACTION");
boolean mult = getIntent().getExtras().getBoolean("MULTIPLICATION");
boolean division = getIntent().getExtras().getBoolean("DIVISION");
答案 3 :(得分:1)
确保使用相同的键来放置和获取数据。
同时更新
boolean subtraction = Boolean.getBoolean("SUBTRACTION");
到
boolean subtraction = b.getBoolean("SUBTRACTION");
答案 4 :(得分:1)
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
从intent中检索布尔值:
Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("BoolName", true);
startActivity(intent );
答案 5 :(得分:1)
我将使用这篇文章来说明为什么 mcve 是有用的,因为这是一个完美的例子,我将使用他的代码,清理无用的部分,我们会很快发现问题,如果你反对这个答案,我也不应该或者要求upvote但不要投票,评论我会在需要时将其删除。
所以,让我们清理代码,我只对这里的Intent
创建感兴趣。因此,请清理其余部分并保留使用的变量/常量。为了最小化,我们只需要一个布尔值,让第一个putBoolean。
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);
}
});
嗯,这样更好。适合一个屏幕,我们直接看到问题。
恢复相同:
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 ......
答案 6 :(得分:1)
我希望代码可以帮到你 Intent intent = new Intent(this,NextActivity.class); intent.putExtra(&#34; yourBoolName&#34;,true);
protected void onCreate(Bundle savedInstanceState) {
Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");
}
答案 7 :(得分:1)
Intent intt = new Intent(this,YourAcivity.class);
intt.putExtra("BooleanKey",true);
startActivity(intt);
protected void onCreate(Bundle savedInstanceState) {
Boolean yourBool = getIntent().getExtras().getBoolean("yourBoolName");
}