package com.zdsoft.togglebutton1228;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
private ToggleButton tb_show;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tb_show = (ToggleButton) findViewById(R.id.tb_show);
img = (ImageView) findViewById(R.id.img);
//方法1
// tb_show.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// if (tb_show.isChecked()){
// img.setImageDrawable(ContextCompat.getDrawable(MainActivity.this,R.drawable.xxl));
// Toast.makeText(MainActivity.this, "接受新消息通知", Toast.LENGTH_SHORT).show();
// }else {
// img.setImageDrawable(ContextCompat.getDrawable(MainActivity.this,R.drawable.xx));
// Toast.makeText(MainActivity.this, "不接受新消息通知", Toast.LENGTH_SHORT).show();
//
// }
// }
// });
//方法2
tb_show.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
tb_show.setChecked(isChecked);
img.setImageResource(isChecked ? R.drawable.xxl : R.drawable.xx);
}
});
}
}
ToggleButton(开关按钮)
最新推荐文章于 2024-09-02 22:00:11 发布