如何在微调器中使用if else语句?

时间:2018-11-26 15:50:55

标签: java android android-studio

这是我的代码,我不知道如何使用带有两个微调器的if else语句 //(spinner1.equals"THB“ && spinner2.equals” USD“)<-之类的

https://drive.google.com/file/d/1e_TbY6e-KAE7Iq6GLq2CFy9WA1VBy1qx/view?fbclid=IwAR1s7C9SVm2S202uAmURjwTilHu2ppwkHclvN7gDT3cEFFwZkqyktZv6MRE

2 个答案:

答案 0 :(得分:1)

如果您要谈论用户选择的微调器的值:

Spinner spinner1 = findViewById(R.id.your_spinner1);
Spinner spinner2 = findViewById(R.id.your_spinner2);

if(spinner1.getSelectedItem().toString().equals("THB") && spinner2.getSelectedItem().toString().equals("USD"){

    //do stuff here

}

就像评论中的用户f1sh一样,将您的代码发布到问题中。

答案 1 :(得分:0)

扩展接口 AdapterView.OnItemSelectedListener

package com.example.acer.spinner;

import android.accessibilityservice.GestureDescription;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import java.lang.ref.SoftReference;

public class MainActivity extends AppCompatActivity {
    Spinner spn,spn1;
    EditText inmoney;
    TextView answer;
    Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    inmoney=findViewById(R.id.text);
    answer=findViewById(R.id.textView);
    btn=findViewById(R.id.button);

    // first spinner
    spn=findViewById(R.id.spinner);
    spn1=findViewById(R.id.spinner2);
    String [] legion ={"THB","USD","EUR","JPY","SGD","MYN","CNY"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,legion);
    spn.setAdapter(adapter);
    spn1.setAdapter(adapter);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            exrate();
        }
    });
}
public void exrate(){
    double num=Double.parseDouble(inmoney.getText().toString());
    if (spn1.getSelectedItem().toString().equals("THB") && spn.getSelectedItem().toString().equals("USD")){
        double ans=num*5;
        answer.setText(String.valueOf(ans));
    }
    else{
        //do other currency conversions
    }
}
}

您还可以使用 其他if(condition)代替其他进行其他比较和转换

在Java行话中被称为梯形图