djc*_*476 0 java android if-statement spinner
因此,我正在尝试编写一个包含两个微调器和一个按钮的活动,当选择了两个微调器并按下按钮时,它将带您进入另一个活动.除了一个组合,它应该产生一个Toast,说你不能这样做.
无论如何,这是代码:
public void onClick(View v) {
String spinnerchoice1 = ("spinner1Value");
String spinnerchoice2 = ("spinner2Value");
if((spinnerchoice1.equals("Walking")) && (spinnerchoice2.equals("Hiking"))){
Toast.makeText(getBaseContext(), "I'm sorry, this is not possible.", Toast.LENGTH_LONG).show();
}else{
Intent i = new Intent(GetDirections.this.getApplicationContext(), DirectionDisplay.class);
i.putExtra("spinner1Value", transportSpinner.getSelectedItem().toString());
i.putExtra("spinner2Value", locationSpinner.getSelectedItem().toString());
GetDirections.this.startActivity(i);
}
}
Run Code Online (Sandbox Code Playgroud)
谁能告诉我哪里出错了?
谢谢
您正在比较两个硬编码字符串,if条件永远不会执行.将代码更改为:
public void onClick(View v) {
String transport = transportSpinner.getSelectedItem().toString();
String location = locationSpinner.getSelectedItem().toString();
if ("Walking".equals(transport) && "Hiking".equals(location)) {
Toast.makeText(getBaseContext(), "I'm sorry, this is not possible.", Toast.LENGTH_LONG).show();
} else {
Intent i = new Intent(GetDirections.this.getApplicationContext(), DirectionDisplay.class);
i.putExtra("spinner1Value", transport);
i.putExtra("spinner2Value", location);
GetDirections.this.startActivity(i);
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
322 次 |
最近记录: |