我又找了好几个小时,没有找到我明白/正在寻找的答案。
我有一个偏好屏幕,当用户单击菜单中的设置时打开。这个很管用。但是,当用户完成设置时,如何最好地使用户能够关闭这个屏幕。
我喜欢Chrome的方式,在这里你可以返回到以前的屏幕。
其他可能性也受到赞赏。
活动,它属于首选项(它应该返回该活动):
public class MainActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startGame(View view)
{
Intent intent = new Intent(this, Game.class);
startActivity(intent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.game_settings, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.action_settings:
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}优惠:
public class SettingsFragment extends PreferenceFragment
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preferences);
}
}XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/game_settings">
<ListPreference
android:title="@string/circle_setting_title"
android:key="circle_setting"
android:summary="@string/circle_setting_summary"
android:entries="@array/circle_setting_amount"
android:entryValues="@array/circle_setting_amount_value"
android:defaultValue="3"/>
<ListPreference
android:title="@string/color_setting_title"
android:key="color_setting"
android:summary="@string/color_setting_summary"
android:entries="@array/color_setting_amount"
android:entryValues="@array/color_setting_amount_value"
android:defaultValue="3"/>
</PreferenceCategory>
</PreferenceScreen>发布于 2014-01-11 14:03:20
要使MainActivity成为首选项的父活动:
假设您已经在res/xml文件夹中拥有您的prefs.xml (或您喜欢命名的任何名称)文件:
在报表中声明您的活动(在应用程序部分中):
<!-- Main activity -->
<activity
android:name="com.example.app.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Preferences -->
<activity
android:name="com.example.app.PrefsActivity"
/>然后,假设您有一个菜单、一个按钮或您想要显示PreferenceScreen的任何东西,只需将这一行添加到事件处理程序中:
startActivity(new Intent(this, PrefsActivity.class));发布于 2016-03-28 14:35:25
接受的答案是不正确的。通过执行:startActivity(new Intent(this, PrefsActivity.class));,您实际上向堆栈中添加了一个新的活动。
如果您想返回一个屏幕,您应该使用onBackPressed方法。
代码示例应如下所示:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
getActivity().onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}上述解决方案将在操作栏上设置主按钮,以与返回按钮的反应相同。这两个按钮将返回活动的堆栈到SettingsActivity (之前的一个活动)。
发布于 2014-08-08 01:49:54
而不是创建一个新的活动,只需覆盖首选项片段的布局,并膨胀布局中的按钮。然后侦听按钮单击的事件,以关闭片段。
https://stackoverflow.com/questions/21062476
复制相似问题