【问题标题】:How to convert a value in a text field (kg to lbs) using a toggle or switch button [closed]如何使用切换或切换按钮将文本字段中的值(kg 到 lbs)转换 [关闭]
【发布时间】:2019-01-09 22:40:50
【问题描述】:

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

【问题讨论】:

  • 请向我们展示您已经尝试过的一些努力。没有人会在这里为您编写完整的代码。
  • 堆栈溢出不是免费的代码设计和编写服务。请向我们展示您的尝试并解释错误的原因。

标签: android xml android-layout togglebutton


【解决方案1】:

你不能。你必须自己通过代码来实现它。

今天心情不错,很无聊,所以我会为你写一些代码,即使你不费吹灰之力。

简而言之,在您的 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));
        }
    }
});

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多