厘米到英尺和英寸

厘米到英尺和英寸

本文介绍了高度转换 - 厘米到英尺和英寸(反之亦然)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个编辑文本,用户可以用厘米以及英尺+英寸输入他的身高,例如。 5'11。我有一个目标单位的切换按钮,所以我希望当用户选择厘米时,它应该将输入的文本从英尺+英寸转换为厘米,反之亦然。
现在当我将高度转换为厘米,最后添加\。我认为这是因为我提出的文本观察者在计数达到3时最后添加\。

I have an edit text in which the user can enter his height in centimeters as well as in feet+inches, eg. 5'11". I have a toggle button for the target unit, so I want that when the user selects centimeters, it should convert the entered text from feet+inches into centimeters, and vice versa. Now when I am converting the height to centimeters it's adding "\"" at the end. I think it's because of the text watcher I have put which add "\"" at the end when count is reaching to 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());
    }
}


推荐答案

这可以通过正则表达式轻松完成,但我认为您应该首先尝试更简单的方法。

This can be easily done with regular expressions, but I think you should try the more straightforward way first.

基本上,格式就像 xx'xx。我们可以使用' deliminator 拆分字符串。这样,数组的第一项是英尺数。

Basically, the format is like xx'xx". We can split the string using the ' deliminator. That way, the first item of the array is the number of feet.

然后,我们剩下拆分字符串的第二项: xx 。为此,我们只需将其子串以删除最后一个字符,然后我们就可以获得英寸数!

Then, we have the second item of the split string left: xx". For this, we just need to substring it to remove the last character and then we can get the number of inches!

尝试自己编写代码!

如果你真的被卡住了,这就是解决方案:

If you're really stuck, here's the solution:

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

这是使用r的解决方案egex:

And here's a solution using regex:

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!

这篇关于高度转换 - 厘米到英尺和英寸(反之亦然)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 02:10