如何使用切换或切换按钮将文本字段中的值(kg 转换为 lbs)

How to convert a value in a text field (kg to lbs) using a toggle or switch button

我找不到这个问题的解决方案。我想使用切换或切换按钮将文本字段的值从一个单位转换为另一个单位。感谢您的宝贵时间!

你不能。你得自己用代码实现。

今天心情不错,有点无聊,写点代码给你,就算你不努力。

简而言之,在您的 XML 中添加 switchTextView/EditText,然后在您的代码中实现如下内容:

Switch mySwitch;
TextView mTextView;

mSwitch = findViewById(R.id.mSwitch);
mTextView = findViewById(R.id.mTextView);

double kg = 0.0;
double lbs = 0.0;
final double change = 2.2046226218; //this is the change between kg and lbs, 1kg = this amount of lbs

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean convertToKg) {
        double currentValue = Double.parseDouble(mTextView.getText().toString());
        if(convertToKg){
            mTextView.setText(String.valueOf(currentValue / change));
        }else{
            mTextView.setText(String.valueOf(currentValue * change));
        }
    }
});

希望这对您有所帮助