该代码是参考android Settings里面在安装app时没有打开“安装未知来源应用” 时根据系统提示跳转至这个开关处:
import android.os.Bundle;
import android.os.Handler;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
public class MainActivity extends PreferenceActivity {
private static final String KEY_DEVICEINFO = "device_info";
int mDeviceInfoCountdown;
char myChar='a';
private CheckBoxPreference ck;
private Preference my;
int deviceInfoPosition;
private Handler mScrollHandler = new Handler();
private Runnable mScrollRunner = new Runnable() {
public void run() {
ListView lastView = getListView();
// move to the visiable position.
lastView.setItemChecked(deviceInfoPosition - 1, true);
lastView.setSelection(deviceInfoPosition - 1);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pre);
ck = (CheckBoxPreference) findPreference("set_visiable");
ck.setOnPreferenceClickListener(new OnPreferenceClickListener() {
@Override
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
if (ck.isChecked()) {
addPreference();//点击一次新增一个Preference使位置增加
getPreferenceScreen().addPreference(my);
deviceInfoPosition = 0;
findPreferencePosition(KEY_DEVICEINFO, getPreferenceScreen());
mScrollHandler.postDelayed(mScrollRunner, 100);
Log.e("info", "check true===="+deviceInfoPosition);
} else {
getPreferenceScreen().removePreference(my);
}
return true;
}
});
my = new Preference(this);
my.setTitle("new add");
my.setKey(KEY_DEVICEINFO);
my.setSummary("new ................");
getPreferenceScreen().addPreference(my);
}
private void addPreference(){
if(myChar<'z'){
String temp = myChar+"";
Preference pf=new Preference(this);
pf.setTitle(temp.toUpperCase());
pf.setKey(temp);
pf.setSummary(temp);
getPreferenceScreen().addPreference(pf);
myChar++;
}
}
private Preference findPreferencePosition(CharSequence key,
PreferenceGroup root) {
if (key.equals(root.getKey())) {
return root;
} else {
deviceInfoPosition++;
}
final int preferenceCount = root.getPreferenceCount();
for (int i = 0; i < preferenceCount; i++) {
final Preference preference = root.getPreference(i);
final String curKey = preference.getKey();
if (curKey != null && curKey.equals(key)) {
return preference;
}
if (preference instanceof PreferenceGroup) {
PreferenceGroup group = (PreferenceGroup) preference;
final Preference returnedPreference = findPreferencePosition(
key, group);
if (returnedPreference != null) {
return returnedPreference;
}
} else {
deviceInfoPosition++;
}
}
return null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
pre.xml
<pre name="code" class="java"><?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="set_VPN_Category"
android:title="jump" >
<CheckBoxPreference
android:key="set_visiable"
android:summaryOff="隐藏"
android:summaryOn="显示"
android:title="跳转" />
</PreferenceCategory>
<PreferenceCategory
android:key="set_net"
android:title="网络设置" >
<CheckBoxPreference
android:key="set_3g"
android:summary="使用3G虽可提高数据载入速度,但可能会降低电池寿命"
android:title="启用3G" />
<CheckBoxPreference
android:key="set_2g"
android:summaryOff="打开蜂窝数据"
android:summaryOn="关闭蜂窝数据"
android:title="蜂窝数据" />
<Preference
android:key="set_2g_net"
android:title="蜂窝数据网络" />
<CheckBoxPreference
android:key="set_roving"
android:summary="在境外时请关闭数据漫游"
android:title="数据漫游" />
</PreferenceCategory>
<PreferenceCategory
android:key="set_VPN_Category"
android:title="VPN设置1" >
<Preference
android:key="set_VPN"
android:title="设置VPN" />
</PreferenceCategory>
<PreferenceCategory
android:key="set_VPN_Category"
android:title="VPN设置2" >
<Preference
android:key="set_VPN"
android:title="设置VPN" />
</PreferenceCategory>
<PreferenceCategory
android:key="set_VPN_Category"
android:title="VPN设置3" >
<Preference
android:key="set_VPN"
android:title="设置VPN" />
</PreferenceCategory>
<PreferenceCategory
android:key="set_VPN_Category"
android:title="VPN设置4" >
<Preference
android:key="set_VPN"
android:title="设置VPN" />
</PreferenceCategory>
</PreferenceScreen>