我想在 2 秒延迟后切换按钮的两种状态
I wanted to toggle between two states of my toggle button after the 2 second delay
我试图每 2 秒更新我的 UI 状态,UI 包括一个文本视图和切换按钮,我的问题是文本视图得到正确更新,但切换按钮得到一开始更新,然后什么也没有发生,这是使用的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb = (ToggleButton) findViewById(R.id.tb);
tv = (TextView) findViewById(R.id.tv);
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
UpdateGUI();
}
}, 0, 2000);
}
private void UpdateGUI() {
i++;
//tv.setText(String.valueOf(i));
myHandler.post(myRunnable);
}
final Runnable myRunnable = new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText("Counter is "+i);
if ((i/2)==0){System.out.println("Condition true........"+i);tb.setChecked(true);}else{ System.out.println("Condition failed........"+i);tb.setChecked(false);}
}
});
}
};
}
logcat中的输出是
System.out: Condition true........1
I/System.out: Condition failed........2
01-07 11:04:49.304 17321-17321/com.example.vishal.updateui I/System.out: Condition failed........3
01-07 11:04:50.305 17321-17321/com.example.vishal.updateui I/System.out: Condition failed........4
01-07 11:04:51.304 17321-17321/com.example.vishal.updateui I/System.out: Condition failed........5
01-07 11:04:52.305 17321-17321/com.example.vishal.updateui I/System.out: Condition failed........6
你的切换代码条件错误
您的代码:
if ((i / 2) == 0) {
System.out.println("Condition true........" + i);
tb.setChecked(true);
} else {
System.out.println("Condition failed........" + i);
tb.setChecked(false);
}
用这个更新:
if ((i % 2) == 0) {
System.out.println("Condition true........" + i);
tb.setChecked(true);
} else {
System.out.println("Condition failed........" + i);
tb.setChecked(false);
}
问题出在“/”上,因为如果 i==1,则值将为 1/2 = 0,但在 i/2.
之后,值将永远不会为 0
我试图每 2 秒更新我的 UI 状态,UI 包括一个文本视图和切换按钮,我的问题是文本视图得到正确更新,但切换按钮得到一开始更新,然后什么也没有发生,这是使用的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb = (ToggleButton) findViewById(R.id.tb);
tv = (TextView) findViewById(R.id.tv);
Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
UpdateGUI();
}
}, 0, 2000);
}
private void UpdateGUI() {
i++;
//tv.setText(String.valueOf(i));
myHandler.post(myRunnable);
}
final Runnable myRunnable = new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText("Counter is "+i);
if ((i/2)==0){System.out.println("Condition true........"+i);tb.setChecked(true);}else{ System.out.println("Condition failed........"+i);tb.setChecked(false);}
}
});
}
};
}
logcat中的输出是
System.out: Condition true........1
I/System.out: Condition failed........2
01-07 11:04:49.304 17321-17321/com.example.vishal.updateui I/System.out: Condition failed........3
01-07 11:04:50.305 17321-17321/com.example.vishal.updateui I/System.out: Condition failed........4
01-07 11:04:51.304 17321-17321/com.example.vishal.updateui I/System.out: Condition failed........5
01-07 11:04:52.305 17321-17321/com.example.vishal.updateui I/System.out: Condition failed........6
你的切换代码条件错误
您的代码:
if ((i / 2) == 0) {
System.out.println("Condition true........" + i);
tb.setChecked(true);
} else {
System.out.println("Condition failed........" + i);
tb.setChecked(false);
}
用这个更新:
if ((i % 2) == 0) {
System.out.println("Condition true........" + i);
tb.setChecked(true);
} else {
System.out.println("Condition failed........" + i);
tb.setChecked(false);
}
问题出在“/”上,因为如果 i==1,则值将为 1/2 = 0,但在 i/2.
之后,值将永远不会为 0