高度转换-厘米到英尺和英寸(反之亦然)



我有一个编辑文本,用户可以在其中输入他的身高,单位是厘米,也可以是英尺+英寸,例如。5"11"。我有一个用于目标单位的切换按钮,所以我希望当用户选择厘米时,它应该将输入的文本从英尺+英寸转换为厘米,反之亦然。现在,当我把高度转换成厘米时,它在最后加上了""。我认为这是因为我在计数达到3时,在末尾添加""的文本监视器。

public void onClick(View view) {
    switch (view.getId())
    {
        case R.id.btnCm:
            toggleHeightButton(R.id.btnCm,R.id.btnFeet,false);
            convertToCentimeter(enter_height);
            break;
        case R.id.btnFeet:
            toggleHeightButton(R.id.btnFeet,R.id.btnCm,true);
            enter_height.addTextChangedListener(new CustomTextWatcher(enter_height));
            break;
        case R.id.btnKg:
            toggleweightButton(R.id.btnKg,R.id.btnpound,false);
            break;
        case R.id.btnpound:
            toggleweightButton(R.id.btnpound,R.id.btnKg,true);
            break;
    }
}
public class CustomTextWatcher implements TextWatcher {
    private EditText mEditText;
    public CustomTextWatcher(EditText enter_height) {
        mEditText = enter_height;
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }
    public void afterTextChanged(Editable s) {
        int count = s.length();
        String str = s.toString();
        if (count == 1) {
            str = str + "'";
        } else if (count == 2) {
            return;
        } else if (count == 3) {
            str = str + """;
        } else if ((count > 4) && (str.charAt(str.length() - 1) != '"') ){
            str = str.substring(0, str.length() - 2) + str.charAt(str.length() - 1) + """;
        } else {
            return;
        }
        mEditText.setText(str);
        mEditText.setSelection(mEditText.getText().length());
    }
}

有一个数学计算来管理厘米到英尺的转换,反之亦然。

public static String feetToCentimeter(String feet){
        double dCentimeter = 0d;
        if(!TextUtils.isEmpty(feet)){
            if(feet.contains("'")){
                String tempfeet = feet.substring(0, feet.indexOf("'"));
                if(!TextUtils.isEmpty(tempfeet)){
                    dCentimeter += ((Double.valueOf(tempfeet))*30.48);
                }
            }if(feet.contains(""")){
                String tempinch = feet.substring(feet.indexOf("'")+1, feet.indexOf("""));
                if(!TextUtils.isEmpty(tempinch)){
                    dCentimeter += ((Double.valueOf(tempinch))*2.54);
                }
            }
        }
        return String.valueOf(dCentimeter);
        //Format to decimal digit as per your requirement
    }
    public static String centimeterToFeet(String centemeter) {
        int feetPart = 0;
        int inchesPart = 0;
        if(!TextUtils.isEmpty(centemeter)) {
            double dCentimeter = Double.valueOf(centemeter);
            feetPart = (int) Math.floor((dCentimeter / 2.54) / 12);
            System.out.println((dCentimeter / 2.54) - (feetPart * 12));
            inchesPart = (int) Math.ceil((dCentimeter / 2.54) - (feetPart * 12));
        }
        return String.format("%d' %d''", feetPart, inchesPart);
    }

这可以很容易地用正则表达式完成,但我认为你应该先尝试更直接的方法。

基本上,格式类似于xx'xx"。我们可以使用'分隔符对字符串进行split。这样,数组的第一项就是英尺数。

然后,我们有第二个项目的分割字符串:xx"。为此,我们只需要将它的子字符串删除最后一个字符,然后我们就可以得到英寸数了!

试着自己写代码!


如果你真的卡住了,这里有一个解决方案:

String str = s.toString();
String[] splitString = str.split("'");
String firstItem = splitString[0];
try {
    int feet = Integer.parseUnsignedInt(firstItem);
    String secondPart = splitString[1].substring(0, splitString[1].length() - 1);
    int inches = Integer.parseUnsignedInt(secondPart);
    // YAY! you got your feet and inches!
    System.out.println(feet);
    System.out.println(inches);
} catch (NumberFormatException e) {
    return;
}

这里有一个使用正则表达式的解决方案:

String str = s.toString();
Pattern pattern = Pattern.compile("(\d+)'((\d+)")?");
Matcher matcher = pattern.matcher(str);
if (!matcher.matches()) {
    return;
}
int feet = Integer.parseUnsignedInt(matcher.group(1));
String inchesStr = matcher.group(3);
int inches = 0;
if (inchesStr != null) {
    inches = Integer.parseUnsignedInt(inchesStr);
}
// YAY! you got your feet and inches!

最新更新