如何从活动(例如通过意图)打开嵌入式PreferenceScreen到特定首选项?
示例:
<PreferenceScreen
...
<!-- opens a subscreen of settings -->
<PreferenceScreen
android:key="sub_menu_key"
android:persistent="false"
android:title="Submenu">
...
<PreferenceCategory
android:key="category_key"
android:title="Category">
...
<Preference
android:key="tos_key"
android:title="Terms of Service" />
...
</PreferenceCategory>
</PreferenceScreen>
...
有没有办法直接打开“服务条款”可见的地方。
答案 0 :(得分:0)
您只需要在onResume()方法中删除指定的Prefernce类。在我的情况下,我使用的是SwitchPreference类,因此代码类似于-SettingsActivity.class
public static class PrivacyPreferenceFragment extends PreferenceFragment
{
public SwitchPreference switchPreference;
@Override
public void onResume() {
super.onResume();
switchPreference = (SwitchPreference) findPreference("privacy_notice_check");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_privacy);
setHasOptionsMenu(true);
}
}
然后在要使用PrefernceFragment值的活动中,只需使用SharedPreference对象调用值并触发它。
如果您想要SharedPrefernce逻辑,请在下面注释。
答案 1 :(得分:-1)
从活动(es:主要活动)中,您可以使用明确的意图
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button goToPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
goToPreference = (Button) findViewById(R.id.goToButton);
goToPreference.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// when you click go to Preference
Intent intent = new Intent( MainActivity.this, UserSettingsActivity.class);
startActivity(intent);
}
});
}
}
布局:activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="it.uniba.di.ivu.di.sms16.gruppox.examplesettings.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go To Preference"
android:id="@+id/goToButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
UserSettingsActivity是一种扩展PreferenceActivity的活动,允许您查看首选项
import android.content.SharedPreferences;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.os.Bundle;
public class UserSettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if(key.equals("lenguage_preference"))
{
// TO DO
}
}
@Override
protected void onResume() {
super.onResume();
getPreferenceManager().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
}
@Override
protected void onPause() {
getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
super.onPause();
}
}
的settings.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="Lenguage">
<ListPreference
android:key="lenguage_preference"
android:title="Change Lenguage"
android:summary="Choose yout language"
android:entries="@array/entries_list_preference2"
android:entryValues="@array/entryvalues_list_preference2"
android:dialogTitle="choose favourite language" />
</PreferenceCategory>
<PreferenceCategory
android:title="Distance">
<ListPreference
android:key="distance_preference"
android:title="Distance"
android:summary="Change distance"
android:entries="@array/entries_list_preference"
android:entryValues="@array/entryvalues_list_preference"
android:dialogTitle="choose favourite distance" />
</PreferenceCategory>
arrays.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="entries_list_preference"
translatable="false">
<item>2 (km)</item>
<item>1 (km)</item>
<item>500 (m)</item>
<item>200 (m)</item>
</string-array>
<string-array name="entryvalues_list_preference"
translatable="false">
<item>2000</item>
<item>1000</item>
<item>500</item>
<item>200</item>
</string-array>
<string-array name="entries_list_preference2"
translatable="false">
<item>Italian</item>
<item>English</item>
<item>French</item>
</string-array>
<string-array name="entryvalues_list_preference2"
translatable="false">
<item>it</item>
<item>en</item>
<item>fr</item>
</string-array>
有没有办法直接打开“服务条款”?
我认为你可以参考How to open or simulate a click on an android Preference, created with XML, programmatically?