我在向我的应用添加设置页面时遇到问题。
我已经创建了一个SettingsActivity,SettingsFragment,一个actibity_settings.xml布局和一个preferences.xml文件。
我在activity_main视图中放置了一个按钮,并向MainActivity添加了一个intent来调用SettingsActivity。
然而,放置按钮似乎无能为力。
SettingsActivity
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences pref;
public static void start(Context context) {
Intent intent = new Intent(context, SettingsActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onDetachedFromWindow() {
pref.unregisterOnSharedPreferenceChangeListener(this);
super.onDetachedFromWindow();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// TODO handle any special logic on change of a preference if you want
}
}
SettingsFragment
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
MainActivity,省略了其余的方法。
public class MainActivity extends Activity implements OnClickListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
...
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor(BAR_COLOUR)));
context = getApplicationContext();
View createButton = findViewById(R.id.locate_button);
createButton.setOnClickListener(this);
latitudeTextView = (TextView) findViewById(R.id.latitude_textview);
latitudeTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.define_location, 0, 0, 0);
latitudeValueTextView = (TextView) findViewById(R.id.latitude_value_textview);
longitudeTextView = (TextView) findViewById(R.id.longitude_textview);
longitudeTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.define_location, 0, 0, 0);
longitudeValueTextView = (TextView) findViewById(R.id.longitude_value_textview);
accuracyTextView = (TextView) findViewById(R.id.accuracy_textview);
accuracyTextView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.info, 0, 0, 0);
accuracyValueTextView = (TextView) findViewById(R.id.accuracy_value_textview);
lastUpdateTextView = (TextView) findViewById(R.id.last_update_view);
lastUpdateValueTextView = (TextView) findViewById(R.id.last_update_date);
webView = (WebView) findViewById(R.id.map_view);
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.setVisibility(View.INVISIBLE);
isPermissionGiven();
}
的preferences.xml
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/pref_catOne"
android:key="pref_key_catOne" >
<CheckBoxPreference
android:key="mockCheckBox"
android:defaultValue="true"
android:title="@string/pref_mockCheckBoxOne"
android:summary="@string/pref_mockCheckBoxSummary" />
<CheckBoxPreference
android:key="mockCheckBox"
android:defaultValue="true"
android:title="@string/pref_mockCheckBoxTwo"
android:summary="@string/pref_mockCheckBoxSummary" />
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_catTwo"
android:key="pref_key_catTwo" >
</PreferenceCategory>
</PreferenceScreen>
activity_settings.xml
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
class="org.softshack.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
main_activity.xml(仅限按钮)
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/accuracy_textview"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/settingsButton" />
答案 0 :(得分:0)
的设置活动应该来自 PreferenceActivity ,而不是活动:
template <typename R, typename... Args>
class C<R(*)(Args...)> {
using func_ptr = R(*)(Args...);
public: bool isLess(func_ptr l, func_ptr r) { return std::less<>{}(l, r); }
};
答案 1 :(得分:0)
以下是设置页面所需的所有部件的完整示例。
为活动activity_settings.xml
创建布局:
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.yourorg.yourapp.package.to.SettingsFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
更新class
attr以反映SettingsFragment
的路径。只需删除它并开始输入,它应该在android studio中自动完成。
创建设置活动SettingsActivity.java
:
public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {
private SharedPreferences pref;
public static void start(Context context) {
Intent intent = new Intent(context, SettingsActivity.class);
context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
pref = PreferenceManager.getDefaultSharedPreferences(this);
pref.registerOnSharedPreferenceChangeListener(this);
}
@Override
public void onDetachedFromWindow() {
pref.unregisterOnSharedPreferenceChangeListener(this);
super.onDetachedFromWindow();
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
// TODO handle any special logic on change of a preference if you want
}
}
创建SettingsFragment.java
:
public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}
答案 2 :(得分:0)
我不是你的点击模式的粉丝,但如果你想保持它只是使用:
View settingsButton = findViewById(R.id. settingsButton);
settingsButton.setOnClickListener(this);