Android 通过单击“首选项”中的显示活动
我想在用户单击我的应用设置中的以下项目时显示活动:Android 通过单击“首选项”中的显示活动,android,android-intent,android-activity,android-preferences,Android,Android Intent,Android Activity,Android Preferences,我想在用户单击我的应用设置中的以下项目时显示活动: <Preference android:title="@string/prefs_about_app" > <intent android:action="com.example.myapp.action.SHOW_ABOUT_DIALOG"/> </Preference> 下面是我在清单中定义活动的方式: <activity android:name=".settin
<Preference android:title="@string/prefs_about_app" >
<intent android:action="com.example.myapp.action.SHOW_ABOUT_DIALOG"/>
</Preference>
下面是我在清单中定义活动的方式:
<activity
android:name=".settings.ShowAboutAppActivity"
android:label="@string/title_activity_show_about_app" >
<intent-filter>
<action android:name="com.example.myapp.action.SHOW_ABOUT_DIALOG"/>
</intent-filter>
</activity>
出了什么问题?好的,解决方案如下: 我从活动中删除了操作检查,然后将清单中的意图过滤器更改为:
<activity
android:name=".settings.ShowAboutAppActivity"
android:label="@string/title_activity_show_about_app" >
<intent-filter>
<action android:name="com.example.myapp.action.SHOW_ABOUT_DIALOG"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
然后在首选项xml中:
<Preference android:title="@string/prefs_about_app" >
<intent android:action="com.example.myapp.action.SHOW_ABOUT_DIALOG"/>
</Preference>
希望这对其他人也有帮助。您所要做的就是在首选项活动或片段中检索首选项,然后覆盖单击时发生的情况。首选项片段和活动有一个名为onPreferenceTreeClick的方法。因此,只需在xml中为您的首选项设置一个键,并在代码中引用它。像这样的 preference.xml
<Preference
android:key="myKey"
android:title="CustomIntentPref"
//...other stuff />
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
if(preference.getKey().equalsIgnoreCase("myKey") {
Intent myIntent = new Intent(PreferenceActivity.this, MyActivity.class);
startActivity(myIntent);
}
}
@凌驾
公共布尔值onPreferenceTreeClick(首选项屏幕首选项屏幕,
优先权(优先权){
if(preference.getKey().equalsIgnoreCase(“myKey”){
Intent myIntent=新的Intent(PreferenceActivity.this、MyActivity.class);
星触觉(myIntent);
}
}
希望有帮助!谢谢这也是可能的。我将其标记为您尝试的答案:D
<Preference android:title="@string/prefs_about_app" >
<intent android:action="com.example.myapp.action.SHOW_ABOUT_DIALOG"/>
</Preference>
<Preference
android:key="myKey"
android:title="CustomIntentPref"
//...other stuff />
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
Preference preference) {
if(preference.getKey().equalsIgnoreCase("myKey") {
Intent myIntent = new Intent(PreferenceActivity.this, MyActivity.class);
startActivity(myIntent);
}
}