Java toggle.setchecked在onTextChanged事件中
我试图使用一个切换按钮来显示在文本框中输入号码时号码检查的状态。我已经用TextView设置了它,这很好,但我想用图形方式替换它。以下内容不会引发错误,但OnChanged事件中的.SetChanged事件不会改变状态:Java toggle.setchecked在onTextChanged事件中,java,android,Java,Android,我试图使用一个切换按钮来显示在文本框中输入号码时号码检查的状态。我已经用TextView设置了它,这很好,但我想用图形方式替换它。以下内容不会引发错误,但OnChanged事件中的.SetChanged事件不会改变状态: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ToggleButton togShortCircuit = (ToggleButton) findViewById(R.id.togShortCircuit);
togShortCircuit.setClickable(false);
final EditText txtOCVListen = (EditText) findViewById(R.id.txtOCV);
txtOCVListen.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
txtOCVListen.setFilters(new InputFilter[]{new CustomRangeInputFilter(0f, 16.0f)});
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(txtOCVListen.getText().toString().equals(null) || txtOCVListen.getText().toString().equals("")){
fltOCV = 0;
} else {
fltOCV = Float.parseFloat(txtOCVListen.getText().toString());
}
//Short Circuit
if (fltOCV < 10.6 && fltULV > 0 && fltULV <= 8.5) {
TextView lblShortCircuitStatus = (TextView) findViewById(R.id.lblShortCircuitStatus);
lblShortCircuitStatus.setText("True");
lblShortCircuitStatus.setTextColor(0xFFD51817); //Red
togShortCircuit.setChecked(true);
} else {
TextView lblShortCircuitStatus = (TextView) findViewById(R.id.lblShortCircuitStatus);
lblShortCircuitStatus.setText("False");
lblShortCircuitStatus.setTextColor(0xFF808080); //Grey
togShortCircuit.setChecked(false);
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终ToggleButton togShortCircuit=(ToggleButton)findViewById(R.id.togShortCircuit);
togShortCircuit.setClickable(错误);
最终编辑文本txtOCVListen=(EditText)findViewById(R.id.txtOCV);
addTextChangedListener(新的TextWatcher(){
@凌驾
更改前文本之前的公共void(字符序列s、int start、int count、int after){
setFilters(新的InputFilter[]{new CustomRangeInputFilter(0f,16.0f)});
}
@凌驾
public void onTextChanged(字符序列、int start、int before、int count){
if(txtOCVListen.getText().toString().equals(null)| | txtOCVListen.getText().toString().equals(“”){
fltOCV=0;
}否则{
fltOCV=Float.parseFloat(txtOCVListen.getText().toString());
}
//短路
如果(fltOCV<10.6&&fltULV>0&&fltULV)在TextWatcher
内的ToggleButton
状态更改没有问题,我甚至在我的代码中尝试了它。您是否调试了它并确保代码通过togShortCircuit.setChecked(true)运行
line?当我进行调试时,它看起来好像正在启动,但状态似乎没有改变。如果我将togShortCircuit.setChecked(true)设置为;在主onCreate主体中,它很好地更改了状态,因此我知道它不是xml。看起来它只是没有传递到对象。另外,有趣的是,如果我在主onCreate中将setChecked设置为true,则语句的其他部分中的setChecked设置为false将触发,并且状态明显地变回false,但仍然不会在if语句中更改回true。