【问题标题】:Launching Activity by using onPreferenceClick使用 onPreferenceClick 启动 Activity
【发布时间】:2012-07-27 14:47:00
【问题描述】:

我正在尝试从我的偏好屏幕启动一项活动,任何帮助将不胜感激。这是我一直在玩的一些代码。

  Preference customerPref2 = (Preference) findPreference("community");
  customerPref2.setOnPreferenceClickListener(new  OnPreferenceClickListener() {

   public boolean onPreferenceClick(Preference preference) {
              Toast.makeText(getBaseContext(),
                "Launching Activity", 
              Toast.LENGTH_SHORT).show();
          Intent intent = new Intent();                                                                 
          intent.setClassName("com.xxx.xxxx", "Community" );
          intent.setClass(Prefs.this, Community.class);
          startActivity(intent);

我不知道要使用这段代码的哪一部分,所以我同时包含了 setClassName 和 setClass

Preference.xml

     <Preference
            android:title="Checkout this Activity"
            android:summary="Launch the Activity"
            android:key="community">

     <intent android:action="android.intent.action.VIEW"
         android:targetPackage="com.xxx.xxxx"
         android:targetClass="com.xxx.xxxx.Community

我还发现人们说要确保您的清单是正确的,因为当我尝试启动该活动时,有时会说它无法找到它。 这是我的清单

   <activity android:name=".Community" android:label="@string/gotodashboard">
        <intent-filter>
            <action android:name="android.intent.category.VIEW" />
            <category android:name="android.intent.category.PREFERENCE" />
        </intent-filter>
    </activity> 

任何帮助或源代码示例都可以帮助我解决这个问题。谢谢你。

【问题讨论】:

    标签: java android android-activity launch preference


    【解决方案1】:

    我已经通过将我的 prefs java 文件中的代码更改为这个来解决这个问题。

     public boolean onPreferenceClick(Preference preference) {
         Intent myIntent = new Intent(Prefs.this, Community.class);
         Prefs.this.startActivity(myIntent);
         return true;
       }
    

    【讨论】:

      猜你喜欢
      • 2011-02-07
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-15
      • 1970-01-01
      • 1970-01-01
      • 2013-08-12
      相关资源
      最近更新 更多