从 PreferenceActivity 启动新活动

发布于 2024-11-29 04:38:00 字数 106 浏览 1 评论 0原文

美好的一天,朋友们。 我有一个 PreferenceActivity,它是从 XML 文件填充的。 当我们按下一项时,我们应该启动新的活动。怎么做呢?我应该在 XML 文件或 Java 类中写什么?

Good day, friends.
I have a PreferenceActivity, it is filled from XML file.
When we press one item, we should launch new activity. How to do it? What should I write in XML-file or in the Java-class?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

原来分手还会想你 2024-12-06 04:38:00

鉴于您使用的是 xml 首选项,您可以将代码直接添加到 xml 中:

<Preference
    android:title="Some Title"
    android:summary="Some Description">

    <intent 
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.package.name"
        android:targetClass="com.package.name.ActivityName"
    />

</Preference>

Given you are using xml preferences you can add code right into the xml:

<Preference
    android:title="Some Title"
    android:summary="Some Description">

    <intent 
        android:action="android.intent.action.VIEW"
        android:targetPackage="com.package.name"
        android:targetClass="com.package.name.ActivityName"
    />

</Preference>
り繁华旳梦境 2024-12-06 04:38:00

添加首选项后,使用

addPreferencesFromResource(R.xml.preferences);

找到要设置 onClick 的首选项

findPreference("foo_bar_pref");

,并通过像这样的转换来定义它

Preference fooBarPref = (Preference) findPreference("foo_bar_pref");

然后您可以轻松地设置其 onClick 使用

fooBarPref.setOnPreferenceClickListener (new OnPreferenceClickListener()){...}

您可以在该侦听器内启动新的 Activity(使用 Intent)。

After you add preferences using

addPreferencesFromResource(R.xml.preferences);

find your preference that you want to set onClick using

findPreference("foo_bar_pref");

and define it by casting like

Preference fooBarPref = (Preference) findPreference("foo_bar_pref");

Then you can easily set its onClick using

fooBarPref.setOnPreferenceClickListener (new OnPreferenceClickListener()){...}

You can start your new Activity (using an Intent) inside that listener.

地狱即天堂 2024-12-06 04:38:00

Gradle 构建者,看这里!

如果您使用 gradle 而不是 ant 作为构建工具,并且您在 android 中声明了 applicationId

[build.gradle]:

android {
    defaultConfig {
        ...
        applicationId "com.overriding.package.name"
    }
    ...
}

这将覆盖您在 AndroidManifest.xmlandroid:package 中声明为应用的唯一标识符的任何值!

[AndroidManifest.xml]

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.package">
    <activity android:name=".settings.MyActivity"/>
</manifest>

必须考虑两个包名称!

<Preference
    android:title="Some Title"
    android:summary="Some Description">

    <intent 
        android:targetPackage="com.overriding.package.name"
        android:targetClass="com.my.package.settings.MyActivity/>
</Preference>

Gradle Builders, Look Over Here!

If you are using gradle over ant as your build tool, and you declared an applicationId inside android.

[build.gradle]:

android {
    defaultConfig {
        ...
        applicationId "com.overriding.package.name"
    }
    ...
}

This will overwrite whatever value you declared in AndroidManifest.xml's android:package as your app's unique identifier!

[AndroidManifest.xml]

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.my.package">
    <activity android:name=".settings.MyActivity"/>
</manifest>

The <intent> would have to take both package names into account!

<Preference
    android:title="Some Title"
    android:summary="Some Description">

    <intent 
        android:targetPackage="com.overriding.package.name"
        android:targetClass="com.my.package.settings.MyActivity/>
</Preference>
荭秂 2024-12-06 04:38:00

这是动态添加首选项的很好的教程...稍后您必须自定义自己的方式。

在 XMl 中:

<Preference  android:key="key" android:title="See Android Market"></Preference>

在 Java 类中:

Preferences preferences=findPreference("key");
               preferences.setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse("https://market.android.com/")));

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* Some initializations */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    ListView listView = new ListView(this);
    listView.setId(android.R.id.list);
    listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1));
    layout.addView(listView);

    this.setContentView(layout);
    /* Preferences time! (we build the preferences) */
    Preference version = getPreference("My School Manager", "Version 2.0",null);
    Preference author = getPreference("Author", "Balu", null);
    Preference marketLink = getPreference("Android market","View all my apps :)",new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/")));

    CheckBoxPreference check = new CheckBoxPreference(this);
    check.setTitle("Checkbox");
    check.setSummary("Example of checkbox");
    DialogPreference license = new MyDialogPreference(this, "License","This is the license for...bla bla");

    /* Now we add the preferences to the preference screen */
    PreferenceScreen preferenceScreen = this.getPreferenceManager()
            .createPreferenceScreen(this);
    addPreferenceCategory(preferenceScreen, "Preferences Tutorial",version, author, marketLink, check, license);
    this.setPreferenceScreen(preferenceScreen);
}
private boolean addPreferenceCategory(PreferenceScreen preferenceScreen,
        String titleCategory, Preference... preferences) {
    boolean addPreference = false;
    for (Preference preference : preferences) {
        if (preference != null)
            addPreference = true;
    }
    if (addPreference) {
        PreferenceCategory preferenceCategory = new PreferenceCategory(this);
        preferenceCategory.setTitle(titleCategory);
        preferenceScreen.addPreference(preferenceCategory);
        for (Preference preference : preferences) {
            if (preference != null)
                preferenceCategory.addPreference(preference);
        }
        return true;
    } else
        return false;
}
private Preference getPreference(String title, String summary, Intent intent) {
    Preference pref = new Preference(this);
    pref.setTitle(title);
    pref.setSummary(summary);
    if (intent != null)
        pref.setIntent(intent);
    return pref;
}

public class MyDialogPreference extends DialogPreference {
    public MyDialogPreference(Context context, String title, String text) {
        super(context, null);
        this.setTitle(title);
        this.setDialogMessage(text);
    }
}

}

This is nice tutorial for add preferences dynamically...later you have to customized its own way.

In XMl :

<Preference  android:key="key" android:title="See Android Market"></Preference>

In Java class:

Preferences preferences=findPreference("key");
               preferences.setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse("https://market.android.com/")));

OR

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.DialogPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceScreen;
import android.widget.LinearLayout;
import android.widget.ListView;

public class SettingsActivity extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    /* Some initializations */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);

    ListView listView = new ListView(this);
    listView.setId(android.R.id.list);
    listView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT, 1));
    layout.addView(listView);

    this.setContentView(layout);
    /* Preferences time! (we build the preferences) */
    Preference version = getPreference("My School Manager", "Version 2.0",null);
    Preference author = getPreference("Author", "Balu", null);
    Preference marketLink = getPreference("Android market","View all my apps :)",new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/")));

    CheckBoxPreference check = new CheckBoxPreference(this);
    check.setTitle("Checkbox");
    check.setSummary("Example of checkbox");
    DialogPreference license = new MyDialogPreference(this, "License","This is the license for...bla bla");

    /* Now we add the preferences to the preference screen */
    PreferenceScreen preferenceScreen = this.getPreferenceManager()
            .createPreferenceScreen(this);
    addPreferenceCategory(preferenceScreen, "Preferences Tutorial",version, author, marketLink, check, license);
    this.setPreferenceScreen(preferenceScreen);
}
private boolean addPreferenceCategory(PreferenceScreen preferenceScreen,
        String titleCategory, Preference... preferences) {
    boolean addPreference = false;
    for (Preference preference : preferences) {
        if (preference != null)
            addPreference = true;
    }
    if (addPreference) {
        PreferenceCategory preferenceCategory = new PreferenceCategory(this);
        preferenceCategory.setTitle(titleCategory);
        preferenceScreen.addPreference(preferenceCategory);
        for (Preference preference : preferences) {
            if (preference != null)
                preferenceCategory.addPreference(preference);
        }
        return true;
    } else
        return false;
}
private Preference getPreference(String title, String summary, Intent intent) {
    Preference pref = new Preference(this);
    pref.setTitle(title);
    pref.setSummary(summary);
    if (intent != null)
        pref.setIntent(intent);
    return pref;
}

public class MyDialogPreference extends DialogPreference {
    public MyDialogPreference(Context context, String title, String text) {
        super(context, null);
        this.setTitle(title);
        this.setDialogMessage(text);
    }
}

}

放低过去 2024-12-06 04:38:00

您必须将 onClickListener 注册到要启动活动的视图。然后,在这个方法中,您只需要有意图地调用该活动即可。像这样的东西:

Intent intent = new Intent(this, ActivityToLaunch.class);

// Start boardgame
startActivity(intent);

You have to register an onClickListener to the view you want to launch the activity. Then, inside this method, you just need to invoke the activity with an intent. Something like this:

Intent intent = new Intent(this, ActivityToLaunch.class);

// Start boardgame
startActivity(intent);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文