0

PreferenceActivity 내의 구성 요소에 대한 실제 참조가 필요합니다. 실제 오브젝트를 변경하지 않습니다,PreferenceActivity 내의 구성 요소에 대한 참조를 얻는 방법

CheckBoxPreference myPref = (CheckBoxPreference) findPreference("setting1"); 
View v = (View)myPref.getView(null, getListView()); 

이 코드는 내가 원하는보기를 반환하지만 실제로 내가 원하는 거기 속성을 변경 것의 복사본입니다.

구성 요소의 일부 속성 (예 : 색상 또는 맞춤)을 변경해야한다는 점에 유의하십시오. 또한 레이아웃을 사용할 수 있지만 몇 가지 이유 때문에 프로그래밍 방식으로이 작업을 수행하려고합니다.

+0

은 "보기"의 ​​전역 변수를 선언 할 수 있습니다 전에 onCreate() 메서드를 호출하고 코드에서 언제든지 해당 속성을 변경합니다. –

답변

0

그림 : this.getListView()는 ListView의 모든 것을 포함하는 ViewGroup을 반환합니다. 아이들을 횡단함으로써 모든 것이 접근 가능합니다.

에서 onCreate 경우에, ListView에가 작성됩니다 하나의 문제는가 AsyncTask를 정의 BT는 (예를 들어 채워지면, 그래서 하나 개의 솔루션은 설문 조사 및 확인하는 것입니다.

public class AsyncChecker extends AsyncTask <Void, Void, Boolean> { 
public interface AsyncCheckerCallbackInterface { 

    boolean checkFunction(); 
    void checkDone(Boolean result); 
} 

private int period; 
private int maxTime; 
final AsyncCheckerCallbackInterface callback; 

public static void Start(int period, int maxTime, AsyncCheckerCallbackInterface callback){ 
    new AsyncChecker(period, maxTime, callback).execute(); 
} 

public AsyncChecker(int period, int maxTime, AsyncCheckerCallbackInterface callback) { 
    this.period = period; 
    this.maxTime = maxTime; 
    this.callback = callback; 
} 

@Override 
protected Boolean doInBackground(Void... arg0) { 
    long startTime = System.currentTimeMillis(); 
    while(System.currentTimeMillis() - startTime < maxTime){ 
     if (callback.checkFunction()) 
      return true; 
     try { 
      Thread.sleep(period); 
     } catch (InterruptedException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
    return false; 
} 

@Override 
protected void onPostExecute(Boolean result) { 
    callback.checkDone(result); 

} 

}

있다
public static void DoIt(final PreferenceActivity pref){ 
    AsyncChecker.Start(10, 1000, new AsyncChecker.AsyncCheckerCallbackInterface() { 
     public boolean checkFunction() { 
      int childCount = pref.getListView().getChildCount(); 
      if (childCount > 0) 
       return true; 
      return false; 
     } 

     public void checkDone(Boolean result) { 
      if (result) 
       new PreferencesRightAligner().Recursive(pref.getListView()); 
     } 
    }); 
} 

private void Recursive(View v){ 
    if (v instanceof CheckBox){ 
     TextView chk = ((TextView) v); 
     ViewGroup father = (ViewGroup)chk.getParent(); 
     ViewGroup grandpa = (ViewGroup)father.getParent(); 
     //if (grandpa.indexOfChild(father) != 1){ 
      grandpa.removeView(father); 
      grandpa.addView(father, 0); 
     //} 
    } 
    else if (v instanceof TextView){ 
     TextView txt = ((TextView) v); 
    } 
    else if (v instanceof RelativeLayout){ 
     RelativeLayout lay = ((RelativeLayout) v); 
     ((RelativeLayout) v).setGravity(Gravity.RIGHT); 
    } 
    if (count!=0 && v instanceof LinearLayout){ 
     LinearLayout lay = ((LinearLayout) v); 
     ((LinearLayout) v).setGravity(Gravity.LEFT); 
    } 
    if (v instanceof ViewGroup){ 
     int childCount = ((ViewGroup)v).getChildCount(); 
     for(int i=0; i<childCount; i++) { 
      View c = ((ViewGroup)v).getChildAt(i); 
      Recursive(c); 
     } 
    } 
} 

그리고에서 onCreate에서 :

DoIt(this); 
관련 문제