gpt4 book ai didi

android - 使用安卓 :action to launch activity from settings

转载 作者:行者123 更新时间:2023-11-30 02:41:03 27 4
gpt4 key购买 nike

我已经定义了我的 settings.xml 文件以包含一个在设置对话框中使用 android:action item 的项目。请参阅下面该 Activity 的示例代码。一切正常。然而,这件事“覆盖”了我的整个 Activity ,当用户按下后退按钮时,我的整个应用程序就完成了。有没有办法在 settings.xml 中使用 android:action 启动“fragment ”,或者我如何重组我的 Activity ,以便在该 Activity 完成后恢复我的主要 Activity ?

<PreferenceScreen>
<Preference android:title="Current User" >
<intent android:action="com.example.coreui.ShowCurrentUserActivity"
/>
</Preference>
</PreferenceScreen>

这是 Activity 代码

public class ShowCurrentUserActivity extends Activity {
public AlertDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
String msgStr;
super.onCreate(savedInstanceState);
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setPositiveButton("Logout",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
ShowCurrentUserActivity.this.finish();
}
});
alert.setNegativeButton("Dismiss",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
ShowCurrentUserActivity.this.finish();
}
});
}
}

这就是我在 AndroidManifest.xml 中指定 Activity 的方式

    <activity
android:name="com.example.coreui.ShowCurrentUserActivity"
android:label="CurrentUser"
android:exported="false">
<intent-filter>
<action android:name="com.example.coreui.ShowCurrentUserActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

最佳答案

如果对话框本身就是一个 Activity ,它就可以正常工作。也可以创建仅显示 fragment 的 Activity 。 (我会尽快编辑我的答案。)

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gyebro.settingsintent" >

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ShowCurrentUserActivity"
android:label="Show Current User"
android:theme="@android:style/Theme.Holo.Light.Dialog">
<intent-filter>
<action android:name="com.gyebro.settingsintent.SHOW_CURRENT_USER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_settings" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

SettingsActivity.java

public class SettingsActivity extends PreferenceActivity {
@Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.xml.pref_headers, target);
}
}

pref_headers.xml

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header android:title="Intent"
android:summary="Launches my Activity">
<intent android:action="com.gyebro.settingsintent.SHOW_CURRENT_USER" />
</header>
</preference-headers>

ShowCurrentUserActivity.java

public class ShowCurrentUserActivity extends Activity {
public AlertDialog dialog = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog);
}
}

最后是 dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do you want to sign out?"
android:layout_margin="20dp"
android:id="@+id/textview" />
<LinearLayout
style="?android:attr/buttonBarButtonStyle"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:weightSum="2">
<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Sign out"
android:id="@+id/button1" />
<Button
style="?android:attr/buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="@+id/button2" />
</LinearLayout>
</LinearLayout>

编辑这是一个示例,当您将对话框 fragment 包装在 Activity 中时。您需要在用户与对话框交互后完成 Activity 。除非你在其他地方也使用Dialog Fragment,否则我建议使用上述解决方案。

ShowCurrentUserActivity.java

public class ShowCurrentUserActivity extends Activity {

private static final String TAG = "ShowCurrentUserActivity";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Show the dialog
ShowCurrentUserDialog fragment = new ShowCurrentUserDialog();
fragment.show(getFragmentManager(), "ShowCurrentUserDialog");
}
public void doPositiveClick() {
// Do stuff here.
Log.d(TAG, "Dialog positive click");
}
public void doNegativeClick() {
// Do stuff here.
Log.d(TAG, "Dialog negative click");
}
public void dialogDetached() {
Log.d(TAG, "Dialog detached, finishing now...");
finish();
}
}

ShowCurrentUserDialog.java

public class ShowCurrentUserDialog extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("Sign out")
.setMessage("Do you want to sign out?")
.setPositiveButton("Sign out",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((ShowCurrentUserActivity)getActivity()).doPositiveClick();
}
}
)
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
((ShowCurrentUserActivity)getActivity()).doNegativeClick();
}
}
)
.create();
}

@Override
public void onDetach() {
super.onDetach();
((ShowCurrentUserActivity)getActivity()).dialogDetached();
}
}

关于android - 使用安卓 :action to launch activity from settings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25752196/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com