【问题标题】:Start an activity from a PreferenceScreen从 PreferenceScreen 开始活动
【发布时间】:2013-03-08 05:51:51
【问题描述】:

我有一个字符串列表(存储在数据库中,而不是作为资源)和
我想允许用户编辑这个列表。这很容易用普通的 Activity,
但在此应用程序中,该列表应该是 user preferences 的一部分。
基本上它是用户想要使用的句子列表。

由于我想提供一致的 UI,因此我想将其添加到首选项屏幕:

<PreferenceScreen xmlns:android="...">
    <!-- Some other categories and preferences here -->
    <PreferenceScreen android:key="PREF_PDT"
                      android:title="Predefined Texts"
                      android:summary="View and edit the list of predefined texts">
    </PreferenceScreen>
    <!-- Some other categories and preferences here -->
<PreferenceScreen>

现在假设我有一个完全可用的Activity,它允许我编辑数据库中的文本,
我该怎么做才能在用户点击“PREF_PDT”项目时使用Activity

我认为我必须对 Activity 进行一些修改
或创建某种自定义首选项视图?

更新:所以为了清楚起见,我不需要“列表”屏幕来挂钩设置,
我只需要给用户一种他们仍然在的印象应用程序的首选项部分(当然不会破坏导航堆栈)。否则他们必须去一个地方编辑一些设置,然后去另一个地方编辑文本。他们希望在“设置”下找到所有内容

更新:我已将问题从“自定义首选项屏幕以编辑项目列表”重命名,因为现在很清楚我要做的是从 PreferenceScreen 启动活动。 sh404 的回答有帮助,但我找不到正确的语法来引用我想要的活动。也许它是 monodroid 特定的。 (ActivityNotFoundException)

【问题讨论】:

标签: android android-layout xamarin.android


【解决方案1】:

这样写:

<Preference
    android:key="mykey"
    android:title="TheTitle"
    android:summary="summary here" >
    <intent android:action="net.hasnath.android.MyActivity"/>
</Preference>

如果你需要传递 Intent Extras:

<Preference
    android:key="mykey"
    android:title="TheTitle"
    android:summary="summary here" >
    <intent android:action="net.hasnath.android.MyActivity">
        <extra android:name="extra_name" android:value="my_value" />
         <extra android:name="extra_2_name" android:value="my_value_2"/>
    </intent>
</Preference>

接下来,您必须在 AndroidManifest.xml 中声明 Activity
或者您可以像这样在首选项屏幕中声明意图:

<intent android:targetPackage="net.hasnath.android"
        android:targetClass="net.hasnath.android.MyActivity"/>

这样您就不必对 AndroidManifest 进行任何更改。

【讨论】:

  • 这显然是我正在寻找的,但我无法正确地得到“动作”部分......告诉我它找不到活动,可能是 Monodroid 特定的语法,正在寻找现在
  • 需要在AndroidManifest.xml中声明activity
  • 嗯,好的...会看看,让我回复你
  • 如果使用 MonoDroid,你不必在清单中声明 Activity,但要注意 android:action 中的名称,它应该是小写的包名,然后是你的任何大小写类名字。
  • @AaronHe 我尝试了所有我能想到的变体,但无法让它发挥作用。最后我使用了更新答案中看到的 targetPackage 和 TargetClass
【解决方案2】:

在清单文件中以这种方式设置活动

<activity android:name=".MainActivity">
     <intent-filter>
            <action android:name=".MainActivity" />
       <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

在偏好文件中

<Preference
     android:key="preferred_key"
     android:title="@string/preferred_title"
     android:summary="@string/preferred_key_summary">
         <intent android:action=".MainActivity"/
</Preference>

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,但我在 stackoverflow 上搜索的解决方案都没有解决我的 activitynotfound 异常。

    这是我从here 找到的可行解决方案:

        <PreferenceScreen  
                    android:title="@string/title_intent_preference"  
                    android:summary="@string/summary_intent_preference">  
    
                <intent android:action="your.action.string"/>  
    
     </PreferenceScreen>  
    

    在 manifest.xml 中的 Activity 中设置意图过滤器

        <activity ...>  
                <intent-filter>  
                    <action android:name="your.action.string"/>  
                    <category android:name="android.intent.category.DEFAULT" />  
                </intent-filter>  
    </activity>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      相关资源
      最近更新 更多