Qui*_*ick 33 android preferenceactivity android-preferences android-activity
美好的一天,朋友们.我有一个PreferenceActivity,它是从XML文件填充的.当我们按下一个项目时,我们应该启动新的活动.怎么做?我应该在XML文件或Java类中编写什么?
gge*_*ish 73
鉴于您使用的是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>
Run Code Online (Sandbox Code Playgroud)
far*_*daj 61
使用后添加首选项后
addPreferencesFromResource(R.xml.preferences);
Run Code Online (Sandbox Code Playgroud)
找到您要在onClick上设置的首选项
findPreference("foo_bar_pref");
Run Code Online (Sandbox Code Playgroud)
并通过铸造来定义它
Preference fooBarPref = (Preference) findPreference("foo_bar_pref");
Run Code Online (Sandbox Code Playgroud)
然后,您可以轻松地设置其onClick使用
fooBarPref.setOnPreferenceClickListener (new OnPreferenceClickListener()){...}
Run Code Online (Sandbox Code Playgroud)
您可以在该侦听器中启动新的Activity(使用Intent).
Som*_*ent 13
如果您使用gradle over ant作为构建工具,并且您声明了applicationId内部android.
[build.gradle]:
android {
defaultConfig {
...
applicationId "com.overriding.package.name"
}
...
}
Run Code Online (Sandbox Code Playgroud)
这将覆盖你的声明任何值AndroidManifest.xml的android:package为您的应用程序的唯一标识符!
[AndroidManifest.xml]
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.package">
<activity android:name=".settings.MyActivity"/>
</manifest>
Run Code Online (Sandbox Code Playgroud)
该<intent>将不得不采取既包名进去!
<Preference
android:title="Some Title"
android:summary="Some Description">
<intent
android:targetPackage="com.overriding.package.name"
android:targetClass="com.my.package.settings.MyActivity/>
</Preference>
Run Code Online (Sandbox Code Playgroud)
Ram*_*ula 12
这是动态添加首选项的好教程...以后您必须自己定制.
在XMl中:
<Preference android:key="key" android:title="See Android Market"></Preference>
Run Code Online (Sandbox Code Playgroud)
在Java类中:
Preferences preferences=findPreference("key");
preferences.setIntent(new Intent(Intent.ACTION_VIEW,Uri.parse("https://market.android.com/")));
Run Code Online (Sandbox Code Playgroud)
要么
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);
}
}
Run Code Online (Sandbox Code Playgroud)
}
| 归档时间: |
|
| 查看次数: |
24697 次 |
| 最近记录: |