Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java .to()方法的用途_Java_Android - Fatal编程技术网

Java .to()方法的用途

Java .to()方法的用途,java,android,Java,Android,我在学习一个教程时遇到了一个.to()方法,该方法使用了几次。我想知道它的目的是什么,它是如何工作的。我试图通过谷歌找到答案,但没有得到我想要的答案。我在下面附上了我的一段代码: public void updateUnitTextFieldUsingTsp(double doubleToConvert, Quantity.Unit currentUnit, Quantity.Unit preferredUnit,

我在学习一个教程时遇到了一个.to()方法,该方法使用了几次。我想知道它的目的是什么,它是如何工作的。我试图通过谷歌找到答案,但没有得到我想要的答案。我在下面附上了我的一段代码:

public void updateUnitTextFieldUsingTsp(double doubleToConvert, Quantity.Unit currentUnit,
                                        Quantity.Unit preferredUnit, TextView targetTextView){


    Quantity currentQuantitySelected = new Quantity(doubleToConvert, currentUnit);

    String tempTextViewText = currentQuantitySelected.to(Quantity.Unit.tsp).to(preferredUnit)
            .toString();

    targetTextView.setText(tempTextViewText);

}
感谢所有的帮助

编辑:

我在下面添加了一些代码,其中包括我的全部主要活动和数量类:

public class Quantity {

final double value;
final Unit unit;

public static enum Unit{

    tsp(1.0d), tbs(0.3333d), cups(0.0208d), oz(0.1666d), pint(0.0104d),
    quart(0.0052d), gallon(0.0013d), pound(0.0125d), ml(4.9289d), liter(0.0049d),
    mg(5687.5d), kg(0.0057d);

    final static Unit baseUnit = tsp;

    final double byBaseUnit;


    private Unit(double inTsp){
        this.byBaseUnit = inTsp;

    }

    public double toBaseUnit(double value){

        return value / byBaseUnit;

    }

    public double fromBaseUnit(double value){

        return value * byBaseUnit;
    }


}

public Quantity(double value, Unit unit){
    super();
    this.value = value;
    this.unit = unit;

}

public Quantity to(Unit newUnit){

    Unit oldUnit = this.unit;
    return new Quantity(newUnit.fromBaseUnit(oldUnit.toBaseUnit(value)), newUnit);


}

@Override
public String toString(){

    DecimalFormat df = new DecimalFormat("#.0000");
    return df.format(value) + " " + unit.name();

}
数量类别:

public class Quantity {

final double value;
final Unit unit;

public static enum Unit{

    tsp(1.0d), tbs(0.3333d), cups(0.0208d), oz(0.1666d), pint(0.0104d),
    quart(0.0052d), gallon(0.0013d), pound(0.0125d), ml(4.9289d), liter(0.0049d),
    mg(5687.5d), kg(0.0057d);

    final static Unit baseUnit = tsp;

    final double byBaseUnit;


    private Unit(double inTsp){
        this.byBaseUnit = inTsp;

    }

    public double toBaseUnit(double value){

        return value / byBaseUnit;

    }

    public double fromBaseUnit(double value){

        return value * byBaseUnit;
    }


}

public Quantity(double value, Unit unit){
    super();
    this.value = value;
    this.unit = unit;

}

public Quantity to(Unit newUnit){

    Unit oldUnit = this.unit;
    return new Quantity(newUnit.fromBaseUnit(oldUnit.toBaseUnit(value)), newUnit);


}

@Override
public String toString(){

    DecimalFormat df = new DecimalFormat("#.0000");
    return df.format(value) + " " + unit.name();

}
}

主要活动:

public class MainActivity extends AppCompatActivity {

private Spinner unitTypeSpinner;
private EditText amountTextView;

TextView teaspoonTextview, tablespoonTextview, cupTextView, ounceTextView,
         pintTextView, quartTextView, gallonTextView, poundTextView,
         milliliterTextView, literTextview, miligramTextView, kilogramTextView;

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

    addItemsToUnitTypeSpinner();

    addListnerToUnitTypeSpinner();

    amountTextView = (EditText) findViewById(R.id.amount_text_view);

    initializeTextViews();

}

public void initializeTextViews(){
    teaspoonTextview = (TextView) findViewById(R.id.tsp_text_view);
    tablespoonTextview = (TextView) findViewById(R.id.tbs_text_view);
    cupTextView = (TextView) findViewById(R.id.cup_text_view);
    ounceTextView = (TextView) findViewById(R.id.oz_text_view);
    pintTextView = (TextView) findViewById(R.id.pint_text_view);
    quartTextView = (TextView) findViewById(R.id.quart_text_view);
    gallonTextView = (TextView) findViewById(R.id.gallon_text_view);
    poundTextView = (TextView) findViewById(R.id.pound_text_view);
    milliliterTextView = (TextView) findViewById(R.id.ml_text_view);
    literTextview = (TextView) findViewById(R.id.liter_text_view);
    miligramTextView = (TextView) findViewById(R.id.mg_text_view);
    kilogramTextView = (TextView) findViewById(R.id.kg_text_view);
}

private void addItemsToUnitTypeSpinner(){
    unitTypeSpinner = (Spinner) findViewById(R.id.unit_type_spinner);

    ArrayAdapter<CharSequence> unitTypeSpinnerAdapter =
            ArrayAdapter.createFromResource(this, R.array.conversion_types,
                    android.R.layout.simple_spinner_item);

    unitTypeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    unitTypeSpinner.setAdapter(unitTypeSpinnerAdapter);
}

public void addListnerToUnitTypeSpinner(){
    unitTypeSpinner = (Spinner) findViewById(R.id.unit_type_spinner);

    unitTypeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            String itemSelectedInSpinner =
                    parent.getItemAtPosition(position).toString();

            checkIfConvertingFromTsp(itemSelectedInSpinner);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            //TODO maybe something here later

        }
    });
}

public void checkIfConvertingFromTsp(String currentUnit) {
    if (currentUnit.equals("teaspoon")) {

        updateUnitTypesUsingTsp(Quantity.Unit.tsp);

    } else {

        if (currentUnit.equals("tabspoon")) {

            updateUnitTypesUsingOther(Quantity.Unit.tbs);

        } else if (currentUnit.equals("cup")) {

            updateUnitTypesUsingOther(Quantity.Unit.cups);
        } else if (currentUnit.equals("ounce")) {

            updateUnitTypesUsingOther(Quantity.Unit.oz);
        } else if (currentUnit.equals("pint")) {

            updateUnitTypesUsingOther(Quantity.Unit.pint);
        } else if (currentUnit.equals("quart")) {

            updateUnitTypesUsingOther(Quantity.Unit.quart);
        } else if (currentUnit.equals(("gallon"))) {

            updateUnitTypesUsingOther(Quantity.Unit.gallon);
        } else if (currentUnit.equals("pound")) {

            updateUnitTypesUsingOther(Quantity.Unit.pound);
        } else if (currentUnit.equals("milliliter")) {

            updateUnitTypesUsingOther(Quantity.Unit.ml);
        } else if (currentUnit.equals("liter")) {

            updateUnitTypesUsingOther(Quantity.Unit.liter);
        } else if (currentUnit.equals("milligram")) {

            updateUnitTypesUsingOther(Quantity.Unit.mg);
        } else {

            updateUnitTypesUsingOther(Quantity.Unit.kg);
        }
    }
}


    public void updateUnitTypesUsingTsp(Quantity.Unit currentUnit){

    double doubleToConvert = Double.parseDouble(amountTextView.getText().toString());

    String teaspoonValueAndUnit = doubleToConvert + " tsp";

    teaspoonTextview.setText(teaspoonValueAndUnit);

    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.tbs, tablespoonTextview);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.cups, cupTextView);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.oz, ounceTextView);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.pint, pintTextView);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.quart, quartTextView);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.gallon, gallonTextView);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.pound, poundTextView);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.ml, milliliterTextView);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.liter, literTextview);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.mg, miligramTextView);
    updateUnitTextFieldUsingTsp(doubleToConvert, Quantity.Unit.kg, kilogramTextView);
}

public void updateUnitTextFieldUsingTsp(double doubleToConvert, Quantity.Unit unitConvertingTo,
                                        TextView theTextView){

    Quantity unitQuantity = new Quantity(doubleToConvert, Quantity.Unit.tsp);

    String temptUnit = unitQuantity.to(unitConvertingTo).toString();
    theTextView.setText(temptUnit);

}

public void updateUnitTypesUsingOther(Quantity.Unit currentUnit){

    double doubleToConvert = Double.parseDouble(amountTextView.getText().toString());

    Quantity currentQuantitySelected = new Quantity(doubleToConvert, currentUnit);

    String valueInTeaspoons = currentQuantitySelected.to(Quantity.Unit.tsp).toString();

    teaspoonTextview.setText(valueInTeaspoons);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.tbs,
            tablespoonTextview);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.cups,
            cupTextView);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.oz,
            ounceTextView);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.pint,
            pintTextView);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.gallon,
            gallonTextView);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.pound,
            poundTextView);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.ml,
            milliliterTextView);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.liter,
            literTextview);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.mg,
            miligramTextView);

    updateUnitTextFieldUsingTsp(doubleToConvert, currentUnit, Quantity.Unit.kg,
            kilogramTextView);

    if(currentUnit.name().equals(currentQuantitySelected.unit.name())){

        String currentUnitTextViewText = doubleToConvert + " " +
                currentQuantitySelected.unit.name();

        String currentTextViewName = currentQuantitySelected.unit.name() + " _text_view";

        int currentId = getResources().getIdentifier(currentTextViewName, "id",
                MainActivity.this.getPackageName());

        TextView currentTextView = (TextView) findViewById(currentId);

        currentTextView.setText(currentUnitTextViewText);

    }

}

public void updateUnitTextFieldUsingTsp(double doubleToConvert, Quantity.Unit currentUnit,
                                        Quantity.Unit preferredUnit, TextView targetTextView){


    Quantity currentQuantitySelected = new Quantity(doubleToConvert, currentUnit);

    String tempTextViewText = currentQuantitySelected.to(Quantity.Unit.tsp).to(preferredUnit)
            .toString();

    targetTextView.setText(tempTextViewText);

}
public类MainActivity扩展了AppCompatActivity{
专用微调器unitTypeSpinner;
私有EditText amountTextView;
TextView茶匙TextView、汤匙TextView、杯TextView、盎司TextView、,
pintTextView、quartTextView、gallonTextView、poundTextView、,
millitertextview、literTextview、miligramTextView、kg textview;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsToUnitTypeSpinner();
addListnerToUnitTypeSpinner();
amountTextView=(EditText)findViewById(R.id.amount\u text\u view);
initializeTextViews();
}
public void initializeTextViews(){
茶匙文本视图=(文本视图)findViewById(R.id.tsp\u text\u视图);
汤匙文本视图=(文本视图)findViewById(R.id.tbs_text_视图);
cupTextView=(TextView)findViewById(R.id.cup\u text\u view);
ounceTextView=(TextView)findViewById(R.id.oz\u text\u视图);
pintTextView=(TextView)findViewById(R.id.pint\u text\u视图);
quartTextView=(TextView)findViewById(R.id.quart\u text\u视图);
gallonTextView=(TextView)findViewById(R.id.galler\u text\u视图);
poundTextView=(TextView)findViewById(R.id.pound\u text\u视图);
millitertextview=(TextView)findViewById(R.id.ml\u text\u视图);
literTextview=(TextView)findViewById(R.id.l\u text\u视图);
miligramTextView=(TextView)findViewById(R.id.mg\u text\u视图);
kg文本视图=(TextView)findViewById(R.id.kg\u text\u视图);
}
私有void addItemsToUnitTypeSpinner(){
unitTypeSpinner=(Spinner)findViewById(R.id.unit\u type\u Spinner);
阵列适配器单元类型SpinnerAdapter=
ArrayAdapter.createFromResource(这是R.array.conversion类型,
android.R.layout.simple\u微调器(项目);
unitTypeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
unitTypeSpinner.setAdapter(unitTypeSpinnerAdapter);
}
public void addListnerToUnitTypeSpinner(){
unitTypeSpinner=(Spinner)findViewById(R.id.unit\u type\u Spinner);
unitTypeSpinner.setOnItemSelectedListener(新的AdapterView.OnItemSelectedListener(){
@凌驾
已选择公共视图(AdapterView父视图、视图视图、整型位置、长id){
字符串项SelectedInSpinner=
parent.getItemAtPosition(position.toString();
检查是否从TSP转换(在微调器中选择项);
}
@凌驾
未选择公共无效(AdapterView父级){
//待会儿再做点什么
}
});
}
public void checkIfConvertingFromTsp(字符串currentUnit){
如果(当前单位等于(“茶匙”)){
更新UnitTypeSusingSP(数量单位tsp);
}否则{
if(当前单位等于(“tabspoon”)){
更新使用其他的单位类型(数量、单位、待定);
}else if(当前单位等于(“杯”)){
更新使用其他的单位类型(数量、单位、杯);
}else如果(当前单位等于(“盎司”)){
更新使用其他单位的单位类型(数量单位:盎司);
}else if(当前单位等于(“品脱”)){
更新使用其他的单位类型(数量、单位、品脱);
}else if(当前单位等于(“夸脱”)){
更新其他单位的单位类型(数量单位夸脱);
}如果(当前单位等于((“加仑”)){
更新使用其他的单位类型(数量、单位、加仑);
}else如果(当前单位等于(“磅”)){
更新使用其他单位的单位类型(数量、单位、磅);
}else如果(当前单位等于(“毫升”)){
更新使用其他的单位类型(数量.单位.ml);
}else如果(当前单位等于(“升”)){
更新使用其他的单位类型(数量、单位、升);
}else如果(当前单位等于(“毫克”)){
更新使用其他单位的单位类型(数量单位:mg);
}否则{
更新使用其他设备的设备类型(数量单位:kg);
}
}
}
public void updateUnitTypeSusingSP(Quantity.Unit currentUnit){
double doubleToConvert=double.parseDouble(amountTextView.getText().toString());
字符串茶匙值和单位=doubleToConvert+“tsp”;
茶匙文本视图.setText(茶匙值和单位);
updateUnitTextFieldUsingTsp(doubleToConvert,Quantity.Unit.tbs,tablespoonTextview);
使用TSP更新UnitTextField(doubleToConvert、Quantity.Unit.cups、cupTextView);
使用TSP(doubleToConvert,Quantity.Unit.oz,盎司文本视图)更新UnitTextField;
使用TSP更新UnitTextField(doubleToConvert,Quantity.Unit.pint,pintTextView);
使用TSP更新UnitTextField(doubleToConvert,Quantity.Unit.quart,quartTextView);
使用TSP更新UnitTextField(双转换,Quantity.Unit.galler,gallentExtView);
使用TSP更新UnitTextField(doubleToConvert,Quantity.Unit.pound,poundTextView);
使用TSP更新UnitTextField(doubleToConvert,Quantity.Unit.ml,milliliterTextView);
使用TSP更新UnitTextField(doubleToConvert,Quantity.Unit.L,literTextview);
使用TSP(doubleToConvert,Quantity.Unit.mg,miligramTextView)更新UnitTextField;
使用TSP(doubleToConvert,Quantity.Unit.kg,kg文本视图)更新UnitTextField;
}
public void UpdateUnitTextField使用TSP(双精度转换,数量。单位转换为,
文本视图(文本视图){
数量单位数量=新数量(doubleToConvert,Quantity.Unit.tsp);
字符串fettUnit=unitQuantity.to(unitConvertingTo).toString();
textView.setText(单位)
String tempTextViewText = currentQuantitySelected
        .to(Quantity.Unit.tsp)
        .to(preferredUnit)
        .toString();
String tempTextViewText = currentQuantitySelected
        .to(preferredUnit)
        .toString();