gpt4 book ai didi

java - 如何连接旋转器与单位转换器一起使用?

转载 作者:行者123 更新时间:2023-12-01 10:30:27 25 4
gpt4 key购买 nike

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

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() 方法是我正在尝试使用的方法。如果你能给我看 1 个例子,我就能弄清楚剩下的事情。

这是我为所有公式制作的 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.

最佳答案

您必须通过选定的指数来计算您的转换。只需向您的微调器添加一个 onitemselected 监听器即可。

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

  2. 为您的类实现接口(interface)

    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.spinner找到选择了哪个微调器。或view.getId()==R.id.spinner1 。最佳实践是使用 switch case。

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

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

了解更多信息:http://developer.android.com/guide/topics/ui/controls/spinner.html

希望对你有帮助

关于java - 如何连接旋转器与单位转换器一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com