从PreferenceActivity启动新活动

时间:2011-08-12 14:08:35

标签: android android-activity android-preferences preferenceactivity

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

5 个答案:

答案 0 :(得分:72)

鉴于您正在使用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>

答案 1 :(得分:60)

使用

添加首选项后
addPreferencesFromResource(R.xml.preferences);

找到您要使用

设置onClick的首选项
findPreference("foo_bar_pref");

并通过像

这样的转换来定义它
Preference fooBarPref = (Preference) findPreference("foo_bar_pref");

然后您可以使用

轻松设置其onClick
fooBarPref.setOnPreferenceClickListener (new OnPreferenceClickListener()){...}

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

答案 2 :(得分:13)

Gradle Builders,看看这里!

如果您使用gradle over ant作为构建工具,并且在applicationId内声明了android

[build.gradle]:

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

这将覆盖您在AndroidManifest.xml的{​​{1}}中声明的任何值作为应用的唯一标识符!

android:package

[AndroidManifest.xml] <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.my.package"> <activity android:name=".settings.MyActivity"/> </manifest> 必须考虑两个包名!

<intent>

答案 3 :(得分:12)

这是一个很好的动态添加首选项的教程...以后你必须自己定制。

在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/")));

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);
    }
}

}

答案 4 :(得分:0)

您必须将onClickListener注册到要启动活动的视图。然后,在此方法中,您只需要使用intent调用活动。像这样:

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

// Start boardgame
startActivity(intent);
相关问题