gpt4 book ai didi

java - 使用 onPreferenceClick 启动 Activity

转载 作者:行者123 更新时间:2023-12-01 15:15:20 25 4
gpt4 key购买 nike

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

  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

首选项.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

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

   <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 文件中的代码更改为此解决了问题。

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

关于java - 使用 onPreferenceClick 启动 Activity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11690182/

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