gpt4 book ai didi

java - 如何使用widget.switch更改editText值Android

转载 作者:行者123 更新时间:2023-12-02 11:59:25 26 4
gpt4 key购买 nike

我有一个 widget.switch,我试图让它根据开关的值将特定数字除以二(或乘以二)。

这基本上是我所拥有的内容,出于隐私原因进行了编辑:

在onCreate中:

theSwtch = (Switch)findViewById(R.id.theSwtch);
theSwtch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (theSwtch.isChecked()){theSwtch.setText("Weekly"); switcher = true;}
else if (!theSwtch.isChecked()){theSwtch.setText("Bi-Weekly"); switcher = false;}
// do something, the isChecked will be
// true if the switch is in the On position
}
});

并在 OnClick 中:

public void onClick(View view) {
Double someNumber = Double.parseDouble(editTextInput.getText().toString());
if (view.getId() == theSwitch.getId()){
if (!theSwitch.isChecked()) {
someNumber = someNumber/2;
editTextInput.setText(someNumber.toString());
} else if (wklySwtch.isChecked()) {
someNumber = someNumber*2;
editTextInput.setText(someNumber.toString());
}
if (view.getId() == btnCalculate.getId()) {
outPut.setText(someNumber.toString());
}
}
}

最佳答案

从 OnClickListener 中删除除法或乘法的逻辑,而是在 OnCheckedChangeListener 中完成整个操作:

    theSwtch = (Switch)findViewById(R.id.theSwtch);
theSwtch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

Double someNumber = Double.parseDouble(editTextInput.getText().toString());
if (theSwtch.isChecked()){
theSwtch.setText("Weekly");
switcher = true;
someNumber = someNumber/2;
editTextInput.setText(someNumber.toString());
} else if (!theSwtch.isChecked()) {
theSwtch.setText("Bi-Weekly");
switcher = false;
someNumber = someNumber*2;
editTextInput.setText(someNumber.toString());
}
// do something, the isChecked will be
// true if the switch is in the On position
}
});

关于java - 如何使用widget.switch更改editText值Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47372770/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com