如何连接微调器以使用单元转换器?

时间:2016-01-31 22:28:03

标签: java android arrays android-studio converter

我有这个项目来制作一个单位转换器,我必须使用一个微调器。在我的代码中,我有一个温度转换器,但我正在使用单选按钮。之后,我设置了另一个单位转换器,将英尺,米,码,英里转换为英尺,米,码,英里。我非常接近完成它但我不知道如何做我的if语句,以便if(如果选择了脚并且在我的第二个微调器上选择了&码)以使其工作。我将提供以下代码。

public class UnitConverterActivity extends AppCompatActivity {
************
EditText temp;
EditText temp2;
RadioButton toC;
RadioButton toF;
Spinner spinner;
Spinner spinner1;
************
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_unit_converter);
    *************************************************
    //casting my xml buttons
    temp = (EditText)findViewById(R.id.TemperatureEditText);
    temp2=(EditText) findViewById(R.id.LengthEditText);
    toC = (RadioButton)findViewById(R.id.toCelsiusRadioButton);
    toF = (RadioButton)findViewById(R.id.toFarenheitRadioButton);
    //casting my xml buttons

    //spinner first
    String[] length={"Feet", "Meters", "Miles", "Yards"};
    ArrayAdapter<String> stringArrayAdapter=
            new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_dropdown_item,
                    length);
    Spinner spinner =
            (Spinner)  findViewById(R.id.firstSpinner);
    spinner.setAdapter(stringArrayAdapter);

    //spinner first

    //spinner second
    String[] length1={"Feet", "Meters", "Miles", "Yards"};
    ArrayAdapter<String> stringArrayAdapter1=
            new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_dropdown_item,
                    length1);
    Spinner spinner1 =
            (Spinner)  findViewById(R.id.secondSpinner);
    spinner1.setAdapter(stringArrayAdapter1);

    //spinner second
    **********************************************

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action",  Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_unit_converter, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
***********************************************************
public void convert(View v){
    double value= new Double(temp.getText().toString());
    if(toC.isChecked()){
            value = UnitConverter.farenheit2Celsius(value);
    }
    else {
        value= UnitConverter.celsius2Farenheit(value);
    }
    temp.setText(new Double(value).toString());

}

public void convertTo(View v){
double value= new Double(temp2.getText().toString());
    if(spinner.isSelected() && spinner1.isSelected()){
        value=UnitConverter.feetToFeet(value);
    }
    temp2.setText(new Double(value).toString());
}
******************************************************
}

*****包围的部分是我的代码。

convertTo()方法正是我想要的工作。 如果你能给我看一个例子,我可以弄清楚其余部分。

这是我为所有公式制作的UnitConverter类。

package com.plamen.unitconverter;


 * Created by Plamen on 1/31/2016.
 */
public class UnitConverter {
public static double celsius2Farenheit(double c){
    return 32+c*9/5;
}
public static double farenheit2Celsius(double f){

    return (f-32)*5/9;
}

//length conversion
public static double feetToFeet(double f){
    return f;
}
public static double feetToMeters(double f){
    return f*0.3058;
}
public static double feetToYards(double f){
    return f*0.333333;
}
public static double feetToMiles(double f){
    return f*0.000189394;
}
public static double yardsToFeet(double y){
    return y/0.333333;
}
public static double yardsToYards(double y){
    return y;
}
public static double yardsToMeters(double y){
    return y*0.9144;
}
public static double yardsToMiles(double y){
    return y*0.000568182;
}
public static double metersToFeet(double m){
    return m*3.28084;
}
public static double metersToYards(double m){
    return m*1.09361;
}
public static double metersToMeters(double m){
    return m;
}
public static double metersToMiles(double m){
    return m*0.000621371;
}
public static double milesToFeet(double miles){
    return miles*5280;
}
public static double milesToYards(double miles){
    return miles*1760;
}
public static double milesToMeters(double miles){
    return miles*1609.34;
}
public static double milesToMiles(double miles){
    return miles;
}
//length conversion
}

This is what my application layout looks like.

1 个答案:

答案 0 :(得分:1)

您必须按所选的indeces计算转换。只需为你的微调器添加一个onitemselected监听器。

  1. 添加OnItemSelectedListener spinner.setOnItemSelectedListener(this); spinner1.setOnItemSelectedListener(this);

  2. 实现您班级的接口

    public MyClass implements OnItemSelectedListener{

  3. 覆盖班级中的OnItemSelectedListener

    @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { *CODE HERE* }

  4. 您可以找到view.getId()==R.id.spinnerview.getId()==R.id.spinner1选择的微调器。最佳做法是切换案例。

  5. int位置将是微调器的选定索引

  6. 现在只需将您的自定义代码和逻辑放入其中即可使用

  7. 有关详细信息:http://developer.android.com/guide/topics/ui/controls/spinner.html

    希望有所帮助