올해는 머신러닝이다.
SwichPerference에 widgetlayout 커스터마이징시 check 설정 본문
내부적으로 android:id를 이용을 하고 있어서 따로 커스트마이징을 해서 지정을 해줘야함..귀찮음..
package com.example.setupproject;
import android.content.Context;
import android.preference.SwitchPreference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.Switch;
public class MySwitchWidget extends SwitchPreference {
private final Listener mListener = new Listener();
private class Listener implements CompoundButton.OnCheckedChangeListener {
@Override
public void onCheckedChanged(
CompoundButton buttonView, boolean isChecked) {
if (!callChangeListener(isChecked)) {
// Listener didn't like it, change it back.
// CompoundButton will make sure we don't recurse.
buttonView.setChecked(!isChecked);
return;
}//if
MySwitchWidget.this.setChecked(isChecked);
}//
}//
public MySwitchWidget(Context context) {
super(context);
init();
}
public MySwitchWidget(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MySwitchWidget(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
setWidgetLayoutResource(R.layout.preference_widget_switch);
}
@Override
public void setWidgetLayoutResource(int widgetLayoutResId) {
// TODO Auto-generated method stub
super.setWidgetLayoutResource(widgetLayoutResId);
}
@Override
protected void onBindView(View view) {
ViewGroup viewGroup= (ViewGroup)view;
clearListenerInViewGroup(viewGroup);
super.onBindView(view);
View checkableView = view.findViewById(R.id.pref_switch);
if (checkableView != null && checkableView instanceof Checkable) {
((Checkable) checkableView).setChecked(isChecked());
if (checkableView instanceof Switch) {
final Switch switchView = (Switch) checkableView;
//switchView.setAccessibilityDelegate();
switchView.setOnCheckedChangeListener(mListener);
}
}
// TODO Auto-generated method stub
super.onBindView(view);
}
private void clearListenerInViewGroup(ViewGroup viewGroup) {
if (null == viewGroup) {
return;
}
int count = viewGroup.getChildCount();
for(int n = 0; n < count; ++n) {
View childView = viewGroup.getChildAt(n);
if(childView instanceof Switch) {
final Switch switchView = (Switch) childView;
switchView.setOnCheckedChangeListener(null);
return;
}else if (childView instanceof ViewGroup){
ViewGroup childGroup = (ViewGroup)childView;
clearListenerInViewGroup(childGroup);
}//else
}
}
}
'Android > Tip&Tech' 카테고리의 다른 글
[펌]App Widget 개발에 필요한 것들 (1) | 2013.06.28 |
---|---|
dialog style에서 actionbar 사용하기 (0) | 2013.06.27 |
[펌]Android BitmapFactory.Options 설명 (0) | 2013.06.18 |
[펌] 나인패치 자동 생성 (0) | 2013.06.14 |
[펌]Android 개발가이드 - View에 할당한 리소스 완전히 해제하기 (0) | 2013.06.13 |