如何在使用微调器对象时使用if else语句?
我希望在Android中make a page like this。
我完成了按钮和微调器的编码,我使用了一个数组来存储微调器中的值。
我的问题:当我在两个微调器中选择一些值并点击“SHOW FARE”按钮时,不会生成票价?
public class HelloSpinner extends Activity implements OnClickListener {
/** Called when the activity is first created.
* @return */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.source_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.destination_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
View v= findViewById(R.id.button);
v.setOnClickListener(this);
if(spinner.getSelectedItem().toString()=="Adarsh Nagar"
&& spinner1.getSelectedItem().toString()=="Rajiv Chowk")
{
TextView t= (TextView) findViewById(R.id.text);
t.setText("Fare:Rs. 16");
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button:
Intent i = new Intent(this,Fare.class);
startActivity(i);
break;
}
}
}
如果有人能在代码中发现错误,我将非常感谢您的反馈!
答案 0 :(得分:0)
为spinner和inside创建setOnItemSelectedListener,将所选值存储在字符串变量中..
然后在按钮中点击检查您是否条件..
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
selectedspinnervalue =s1.getSelectedItem().toString();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
其中字符串变量selectedspinnervalue包含由微调器选择的值..
现在在Button Click中检查如下条件:
yourbuttonobject.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(selectedvalueinspinner.equalsIgnoreCase("Adarsh Nagar")&& selectedvalueinspinner1.equalsIgnoreCase("Rajiv Chowk")){
// Your desired Code..
}
}
});