Java 长度转换器android studio

Java 长度转换器android studio,java,android,unit-conversion,Java,Android,Unit Conversion,我正在尝试制作一个长度转换器应用程序,我的困惑是如何使用两个微调器和一个计算按钮完成从一个单位到另一个单位的转换?我遇到的问题是,我无法理解如何将下拉列表中的单位从一个微调器转换为第二个微调器中的另一个单位。我还需要在用户输入数字并从列表中选择单位后显示答案 这是我主要活动的布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.c

我正在尝试制作一个长度转换器应用程序,我的困惑是如何使用两个微调器和一个计算按钮完成从一个单位到另一个单位的转换?我遇到的问题是,我无法理解如何将下拉列表中的单位从一个微调器转换为第二个微调器中的另一个单位。我还需要在用户输入数字并从列表中选择单位后显示答案

这是我主要活动的布局文件

 <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.unitconverter.MainActivity">

<Button
    android:text="WEIGHT"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:onClick=""
    android:layout_below="@+id/lengthButton"
    android:layout_alignRight="@+id/lengthButton"
    android:layout_alignEnd="@+id/lengthButton"
    android:id="@+id/weightButton"
    tools:ignore="HardcodedText" />

<Button
    android:text="LENGTH"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/lengthButton"
    android:layout_marginTop="164dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    tools:ignore="HardcodedText" />

<TextView
    android:text="Unit Converter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="76dp"
    android:id="@+id/textView"
    android:textSize="36sp"
    android:textColor="@android:color/background_dark"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<Button
    android:text="VOLUME"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/volumeButton"
    tools:ignore="HardcodedText"
    android:layout_below="@+id/weightButton"
    android:layout_alignLeft="@+id/weightButton"
    android:layout_alignStart="@+id/weightButton" />

<Button
    android:text="TEMPERATURE"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/temperatureButton"
    tools:ignore="HardcodedText"
    android:layout_below="@+id/volumeButton"
    android:layout_centerHorizontal="true" />
         public class MainActivity extends AppCompatActivity {

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



        Button lengthbtn = (Button) findViewById(R.id.lengthButton);

        lengthbtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                startActivity(new Intent(MainActivity.this, lengthActivity.class));
            }
        });

        Button weightbtn = (Button) findViewById(R.id.weightButton);

        weightbtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                startActivity(new Intent(MainActivity.this, weightActivity.class));
            }
        });

        Button volumebtn = (Button) findViewById(R.id.volumeButton);

        volumebtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
               startActivity(new Intent(MainActivity.this,volumeActivity.class));
            }
        });

        Button temperaturebtn = (Button) findViewById(R.id.temperatureButton);

        temperaturebtn.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                startActivity(new Intent(MainActivity.this,temperatureActivity.class));
            }
        });

    }
}
这是我的长度布局文件

 <?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_length"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.unitconverter.lengthActivity">

<Spinner
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:id="@+id/lengthList2"
    android:textAlignment="center"
    android:layout_marginLeft="11dp"
    android:layout_marginStart="11dp"
    android:layout_below="@+id/convertToTextView"
    android:layout_alignLeft="@+id/convertToTextView"
    android:layout_alignStart="@+id/convertToTextView" />

<TextView
    android:text="Length Converter"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="@style/TextAppearance.AppCompat.Display2"
    android:textSize="36sp"
    android:textAlignment="inherit"
    android:id="@+id/lengthTextView"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="25dp"
    android:layout_marginStart="25dp" />

<Button
    android:text="Calculate"
    android:id="@+id/calculateButton"
    android:layout_width="100dp"
    android:layout_height="45dp"
    android:layout_marginTop="53dp"
    android:layout_below="@+id/lengthList2"
    android:layout_alignRight="@+id/lengthList2"
    android:layout_alignEnd="@+id/lengthList2"
    android:layout_marginRight="23dp"
    android:layout_marginEnd="23dp"
    android:textAlignment="center" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/resultTextView"
    android:text="Result "
    android:textSize="20sp"
    android:layout_marginBottom="34dp"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/lengthList2"
    android:layout_alignStart="@+id/lengthList2" />

<TextView
    android:text="Convert to:"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="24sp"
    android:layout_marginTop="114dp"
    android:id="@+id/convertToTextView"
    android:layout_below="@+id/convertEditText"
    android:layout_alignLeft="@+id/convertEditText"
    android:layout_alignStart="@+id/convertEditText" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="Convert from"
    android:ems="10"
    android:id="@+id/convertEditText"
    android:layout_below="@+id/lengthTextView"
    android:layout_alignLeft="@+id/lengthTextView"
    android:layout_alignStart="@+id/lengthTextView"
    android:layout_marginLeft="11dp"
    android:layout_marginStart="11dp"
    android:layout_marginTop="52dp" />

<Spinner
    android:layout_height="wrap_content"
    android:entries="@array/lengthList"
    android:id="@+id/lengthList"
    android:layout_width="200dp"
    android:layout_marginTop="13dp"
    android:layout_below="@+id/convertEditText"
    android:layout_centerHorizontal="true" />
public class lengthActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate (saved Instance State);
        setContentView(R.layout.activity_length);

        final Spinner group = (Spinner)findViewById(R.id.lengthList);
        final Spinner group2 = (Spinner)findViewById(R.id.lengthList2);

        Button button = (Button)findViewById(R.id.calculateButton);

        button.setOnClickListener(new View.OnClickListener() {
            final TextView result = ((TextView) 



            @Override
            public void onClick(View v) {



            }

您好,我已经创建了一个长度转换器。。我希望有帮助

//java file

public class LengthConverter{

EditText val;
TextView ans, ftv;
Spinner from, to;
Integer positionFrom, positionTO, res;
Button convert;

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

    val = (EditText)findViewById(R.id.editText2);

    ans = (TextView)findViewById(R.id.Answer);

    from = (Spinner)findViewById(R.id.SpinnerFrom);
    to = (Spinner)findViewById(R.id.SpinnerTo);

    convert = (Button)findViewById(R.id.convert);


//        positionFrom = from.getSelectedItemPosition();
//        positionTO = to.getSelectedItemPosition();


    List<String> unitlength = new ArrayList<String>();

    unitlength.add("Meter");
    unitlength.add("Centimeter");
    unitlength.add("Kilometer");
    unitlength.add("Miles");
    unitlength.add("Foot");
    unitlength.add("Inches");
    unitlength.add("Yard");

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, unitlength);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    from.setAdapter(dataAdapter);

    ArrayAdapter<String> unit = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, unitlength);
    unit.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    to.setAdapter(unit);

    from.setOnItemSelectedListener(this);

    to.setOnItemSelectedListener(this);


    convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            method(v);
        }
    });
}

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

    parent.getItemAtPosition(position);

    switch (parent.getId())
    {
        case R.id.SpinnerFrom:
            from.setSelection(position);
            positionFrom = from.getSelectedItemPosition();
            Log.e("from selected position:", String.valueOf(positionFrom));
            break;

        case R.id.SpinnerTo:
            to.setSelection(position);
            positionTO = to.getSelectedItemPosition();
            Log.e("to selected position:", String.valueOf(positionTO));
            break;
    }

}


@Override
public void onNothingSelected(AdapterView<?> parent) {

}

public void method(View view)
{

     double value =  Double.parseDouble(val.getText().toString());
     double res;
     double hun = 100;

    Log.e("from value in method", String.valueOf(positionFrom));

    if (positionFrom == 0)
    {
        if (positionTO == 0)
        {
            val.setText("");
            ans.setText("");
        }

        if (positionTO == 1)
        {
            res = value * hun;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 2)
        {
            res = value * 1000;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 3)
        {
            res = value * 0.000621371;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 4)
        {
            res = value * 3.28084;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 5)
        {
            res = value * 39.3701;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 6)
        {
            res = value * 1.09361;
            ans.setText(Double.toString(res));
        }
    }

    if (positionFrom == 1)
    {
        if (positionTO == 0)
        {
            res = value / 100;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 1)
        {
            val.setText("");
            ans.setText("");
        }

        if (positionTO == 2)
        {
            res = value / 10000;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 3)
        {
            res = value * 6.2137e-6;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 4)
        {
            res = value * 0.0328084;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 5)
        {
            res = value * 0.393701;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 6)
        {
            res = value * 0.0109361;
            ans.setText(Double.toString(res));
        }
    }

    if ( positionFrom == 2)
    {
        if (positionTO == 0)
        {
            res = value * 1000;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 1)
        {
            res = value * 100000;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 2)
        {
            val.setText("");
            ans.setText("");
        }

        if (positionTO == 3)
        {
            res = value * 0.621371;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 4)
        {
            res = value * 3280.84;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 5)
        {
            res = value * 39370.1;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 6)
        {
            res = value * 1093.61;
            ans.setText(Double.toString(res));
        }
    }

    if (positionFrom == 3)
    {
        if (positionTO == 0)
        {
            res = value * 1609.34;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 1)
        {
            res = value * 160934;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 2)
        {
            res = value * 1.60934;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 3)
        {
            val.setText("");
            ans.setText("");
        }

        if (positionTO == 4)
        {
            res = value * 5280;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 5)
        {
            res = value * 63360 ;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 6)
        {
            res = value * 1760;
            ans.setText(Double.toString(res));
        }
    }

    if (positionFrom == 4)
    {
        if (positionTO == 0)
        {
            res = value * 0.3048;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 1)
        {
            res = value * 30.48;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 2)
        {
            res = value * 0.0003048;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 3)
        {

            res = value * 0.000189394;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 4)
        {
            val.setText("");
            ans.setText("");
        }

        if (positionTO == 5)
        {
            res = value * 12 ;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 6)
        {
            res = value * 0.333333;
            ans.setText(Double.toString(res));
        }
    }

    if (positionFrom == 5)
    {
        if (positionTO == 0)
        {
            res = value * 0.0254;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 1)
        {
            res = value * 2.54;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 2)
        {
            res = value * 2.54e-5;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 3)
        {
            res = value * 1.5783e-5;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 4)
        {
            res = value * 0.0833333 ;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 5)
        {
            val.setText("");
            ans.setText("");

        }

        if (positionTO == 6)
        {
            res = value * 0.0277778;
            ans.setText(Double.toString(res));
        }
    }

    if (positionFrom == 6)
    {
        if (positionTO == 0)
        {
            res = value * 0.9144;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 1)
        {
            res = value * 91.44;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 2)
        {
            res = value * 0.0009144;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 3)
        {

            res = value * 0.000568182;
            ans.setText(Double.toString(res));

        }

        if (positionTO == 4)
        {
            res = value * 3;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 5)
        {
            res = value * 36 ;
            ans.setText(Double.toString(res));
        }

        if (positionTO == 6)
        {
            val.setText("");
            ans.setText("");

        }
    }

}

}
//java文件
公共类长度转换器{
编辑文本val;
文本视图ans,ftv;
旋转器从,到;
整数positionFrom,positionTO,res;
按钮转换;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u length\u转换器);
val=(EditText)findViewById(R.id.editText2);
ans=(文本视图)findViewById(R.id.Answer);
from=(微调器)findViewById(R.id.SpinnerFrom);
to=(微调器)findViewById(R.id.SpinnerTo);
convert=(按钮)findviewbyd(R.id.convert);
//positionFrom=from.getSelectedItemPosition();
//positionTO=to.getSelectedItemPosition();
List unitlength=new ArrayList();
单位长度。添加(“米”);
单位长度。加上(“厘米”);
单位长度。加上(“公里”);
单位长度。添加(“英里”);
单位长度。添加(“英尺”);
单位长度。添加(“英寸”);
单位长度。添加(“码”);
ArrayAdapter dataAdapter=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项,unitlength);
dataAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉项);
from.setAdapter(dataAdapter);
ArrayAdapter unit=新的ArrayAdapter(这是android.R.layout.simple\u微调器\u项,unitlength);
unit.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
至。设置适配器(单位);
from.setOnItemSelectedListener(此);
to.setOnItemSelectedListener(此);
convert.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
方法(五);
}
});
}
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
parent.getItemAtPosition(位置);
开关(parent.getId())
{
案例R.id.喷丝头来自:
选举产生(职位);
positionFrom=from.getSelectedItemPosition();
Log.e(“从所选位置:”,String.valueOf(positionFrom));
打破
案例R.id.喷丝头:
选举(职位);
positionTO=to.getSelectedItemPosition();
Log.e(“到所选位置:”,String.valueOf(positionTO));
打破
}
}
@凌驾
未选择公共无效(AdapterView父级){
}
公共无效方法(视图)
{
double value=double.parseDouble(val.getText().toString());
双res;
双匈奴=100;
Log.e(“方法中的from值”,String.valueOf(positionFrom));
if(positionFrom==0)
{
if(positionTO==0)
{
val.setText(“”);
ans.setText(“”);
}
如果(位置到==1)
{
res=值*hun;
ans.setText(Double.toString(res));
}
如果(位置TO==2)
{
res=值*1000;
ans.setText(Double.toString(res));
}
如果(位置TO==3)
{
res=值*0.000621371;
ans.setText(Double.toString(res));
}
如果(位置TO==4)
{
res=值*3.28084;
ans.setText(Double.toString(res));
}
如果(位置TO==5)
{
res=值*39.3701;
ans.setText(Double.toString(res));
}
如果(位置TO==6)
{
res=值*1.09361;
ans.setText(Double.toString(res));
}
}
if(positionFrom==1)
{
if(positionTO==0)
{
res=每100个值;
ans.setText(Double.toString(res));
}
如果(位置到==1)
{
val.setText(“”);
ans.setText(“”);
}
如果(位置TO==2)
{
res=价值/10000;
ans.setText(Double.toString(res));
}
如果(位置TO==3)
{
res=值*6.2137e-6;
ans.setText(Double.toString(res));
}
如果(位置TO==4)
{
res=值*0.0328084;
ans.setText(Double.toString(res));
}
如果(位置TO==5)
{
res=值*0.393701;
ans.setText(Double.toString(res));
}
如果(位置TO==6)
{
res=值*0.0109361;
ans.setText(Double.toString(res));
}
}
if(positionFrom==2)
{
if(positionTO==0)
{
res=值*1000;
ans.setText(Double.toString(res));
}
如果(位置到==1)
{
res=价值*100000;
ans.setText(Double.toString(res));
}
如果(位置TO==2)
{
val.setText(“”);
ans.setText(“”);
}
如果(位置TO==3)
{
res=值*0.621371;
ans.setText(Double.toString(res));
}
如果(位置TO==4)
{
res=值*3280.84;
ans.setText(Double.toString(res));
}
如果(位置TO==5)
{
res=数值*39370.1;
ans.setText(Double.toString(res));
}
如果(位置TO==6)
{
res=数值*1093.61;
ans.setText(Double.toString(res));
}
}
如果(位置自==3)
{
if(positionTO==0)
{