Android 当活动启动时,将toogle按钮设置为打开状态
我用一个toogle按钮来启动和停止一个对象,它工作得很好,我需要做的就是在活动开始时将toogle状态设置为ONAndroid 当活动启动时,将toogle按钮设置为打开状态,android,eclipse,Android,Eclipse,我用一个toogle按钮来启动和停止一个对象,它工作得很好,我需要做的就是在活动开始时将toogle状态设置为ON toggleButton.setChecked(true); 这是我的密码: private void animationFart() { ImageView imageanimate = (ImageView) findViewById(R.id.imageView1); imageanimate.setBackgroundResource(R.drawable
toggleButton.setChecked(true);
这是我的密码:
private void animationFart() {
ImageView imageanimate = (ImageView) findViewById(R.id.imageView1);
imageanimate.setBackgroundResource(R.drawable.ball_animation);
animation = (AnimationDrawable) imageanimate.getDrawable();
if (animation.isRunning()) {
animation.stop();
}
animation.start();
}
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable vibrate
animationFart();
}
else {
// Disable vibrate
if (animation.isRunning()) {
animation.stop();
}
}
}
toggleButton.setChecked(true);
在活动的onCreate()
中
start= (ToggleButton) findViewById(R.id.switch1);
start.toggle();
// Enable vibrate
animationFart(); // Start the Animation as Activity starts
toggleButton.setChecked(true);
将视图的选中状态更改为与当前视图相反的状态
陈述
toggleButton.setChecked(true);
默认情况下,ToggleButton将处于关闭状态。所以,您只需切换它
toggleButton.setChecked(true);
或者您可以使用toggleButton.setChecked(true)代码>在活动开始后使用此选项
toggleButton.setChecked(true);
我不明白你的意思,我应该把这行放在哪里?应用程序启动时,toogle按钮会打开,但它不会播放动画,动画会停止。你正在onClick中调用animatiom方法&如果ToggleButton仅打开。当你开始活动时,它会打开,当你点击它时,它会关闭。那么你如何开始动画。按逻辑做一些更改。对此有什么建议吗?你期望的输出是什么?ToggleButton应该如何工作?实际上可以在xml中执行此操作。@Aleem Ahmed您检查了吗?当应用程序启动时,toogle按钮会显示,但它没有播放动画,动画会停止。