toggleButton

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wulianwangxuchenqi/article/details/79915589

开关转换图片

toggleButton

package cn.edu.xynu.computer.hello1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
    private ToggleButton toggleButton;
    private ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lay1);
        toggleButton = findViewById(R.id.toggle01);
        imageView = findViewById(R.id.imageView2);

        myCompoundListener myComListener = new myCompoundListener();
        toggleButton.setOnCheckedChangeListener(myComListener);
    }
    private class myCompoundListener implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            if(compoundButton.getId() == R.id.toggle01){
                if(compoundButton.isChecked()){
                    imageView.setImageResource(R.drawable.aaa);
                }else{
                    imageView.setImageResource(R.drawable.bbb);
                }
            }
        }
    }

}

猜你喜欢

转载自blog.csdn.net/wulianwangxuchenqi/article/details/79915589