import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private MySwitchButton mySwitchButton;
private Button myBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupViews();
}
private void setupViews() {
mySwitchButton = (MySwitchButton) findViewById(R.id.my_switch_button);
//传图片给自定义组件
mySwitchButton.setImageResource(R.drawable.switch_bkg_switch,
R.drawable.switch_btn_slip);
myBtn = (Button) findViewById(R.id.button);
//设置开关状态监听
mySwitchButton.setOnSwitchStateListener(new OnSwitchListener() {
@Override
public void onSwitched(boolean isSwitchOn) {
if (isSwitchOn) {
Toast.makeText(MainActivity.this, "开关开启",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "开关关闭",
Toast.LENGTH_SHORT).show();
}
}
});
myBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean isOn = mySwitchButton.getSwitchState();
mySwitchButton.setSwitchState(!isOn);
}
});
}
}
最后附上Android之实现滑动开关组件代码的下载地址:
------------------------------------------分割线------------------------------------------
具体下载目录在 /2014年资料/12月/15日/Android之实现滑动开关组件
------------------------------------------分割线------------------------------------------