簡體   English   中英

使用onPreferenceClick啟動活動

[英]Launching Activity by using onPreferenceClick

我正在嘗試從我的首選項屏幕啟動一項活動,非常感謝您的幫助。 這是我一直在玩的一些代碼。

  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> 

任何幫助或源代碼示例都可以幫助我解決這一問題。 謝謝你。

我已經通過將我的prefs java文件中的代碼添加到此解決了問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM