Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/194.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 高度转换-厘米到英尺和英寸(反之亦然)_Java_Android - Fatal编程技术网

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

Java 高度转换-厘米到英尺和英寸(反之亦然),java,android,Java,Android,我有一个编辑文本,用户可以在其中以厘米和英尺+英寸输入他的高度,例如5'11“。我有一个目标单位的切换按钮,所以我希望当用户选择厘米时,它应该将输入的文本从英尺+英寸转换为厘米,反之亦然。 现在,当我将高度转换为厘米时,它会在末尾添加“\”。我想这是因为我放了一个文本观察程序,当计数达到3时,它会在末尾加上“\” public void onClick(View view) { switch (view.getId()) { case R.id.btnCm:

我有一个编辑文本,用户可以在其中以厘米和英尺+英寸输入他的高度,例如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());
    }
}

使用正则表达式很容易做到这一点,但我认为您应该首先尝试更直接的方法

基本上,格式类似于
xx'xx”
。我们可以使用
清除器
拆分
字符串。这样,数组的第一项是英尺数

然后,我们剩下分割字符串的第二项:
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!

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

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);
    }

如果
dCentimeter
=91.44、182.88等,则
cm-tofeet
不起作用。