【问题标题】:How to get a reference to components inside PreferenceActivity如何获取对 PreferenceActivity 中组件的引用
【发布时间】:2014-06-16 10:18:51
【问题描述】:

我需要获得对 PreferenceActivity 中组件的真实引用。

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

此代码返回我想要的视图,但它实际上是我想要的内容的副本,并且在那里更改属性,不会更改真实对象。

请注意,我需要更改那里组件的一些属性(例如颜色或对齐方式)。 而且我也知道我可以使用 Layout,但由于某些原因,我想以编程方式完成它

【问题讨论】:

  • 您可以在 onCreate() 方法之前声明一个全局变量 "View" ,并在代码中的任何位置更改其属性。

标签: android sharedpreferences preferenceactivity preference getview


【解决方案1】:

想通了:this.getListView() 返回一个 ViewGroup,其中包含 ListView 中的所有内容。通过遍历孩子,一切都可以访问。

只有一个问题,在 onCreate 事件中,ListView 会被填满,所以一种解决方案是轮询并检查它是否被填满(例如 bt 定义一个 AsyncTask。

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);

【讨论】:

    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 2020-02-12
    • 1970-01-01
    • 2016-08-03
    • 2019-01-06
    • 1970-01-01
    相关资源
    最近更新 更多