我想在用户点击它时更改listview项的背景.有点像Honeycomb设置页面(虽然我没有处理只是设置所以我没有使用PreferenceActivity)我有这个功能通过资源状态选择器状态选择器工作,除了单击列表视图菜单更改线性的情况列表视图右侧的布局(一种分屏视图).我猜测listview失去焦点所以state_pressed不再是真的.
<item android:state_pressed="true">
<shape >
<solid android:color="@color/blue1" />
</shape>
</item>
Run Code Online (Sandbox Code Playgroud)
在选择另一个列表视图项目之前,是否保持列表视图项目着色的任何提示?谢谢!
编辑:
我能够在setOnItemClickListener中更改背景
view.setBackgroundResource(R.color.red);
Run Code Online (Sandbox Code Playgroud)
我只需要一次一个选择,从而被点击其他列表项目时,我试图lv.invalidate()和lv.getChildAt(0).invalidate(),但既不工作和第二导致空指针异常.有什么想法让颜色回来?
我正在尝试从PreferenceActivity开始一个新的Activity.但是,它失败了"无法找到显式活动类.您是否在AndroidManifest.xml中声明了此活动?"
嗯,是的,我宣布:
<application [......] android:debuggable="true">
<activity android:name=".AlarmSettings"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FlashLightActivity"></activity>
<receiver
android:name="AlarmReceiver"
android:process=":remote" >
</receiver>
</application>
Run Code Online (Sandbox Code Playgroud)
AlarmSettings是我的PreferenceActivity.AlarmSettings以这种方式调用FlashLightActivity:
Intent i = new Intent(AlarmSettings.this, FlashLightActivity.class);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)
这是FlashLightActivity:
public class FlashLightActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flashlight);
}
}
Run Code Online (Sandbox Code Playgroud)
... 我不知道该怎么办.有任何想法吗?
是否SwitchPreference在android-support-v4库中兼容ICS?我正在尝试更新一些旧项目,并希望尽可能使用SwitchPreferences.
我知道我可以创建一个单独的资源文件来区分API版本,但我想尽可能避免这种情况.
我已经为PreferenceScreen创建了一个基于xml的PreferenceActivity.在xml中,您可以为不同的首选项指定默认值.但是在打开和关闭屏幕之前,这些不会存储在我的SharedPreferences中.
问题是我想立即使用存储在此屏幕中的首选项(如服务器地址),如果用户想要更改默认值,则只需要打开它.
有没有办法存储偏好屏幕xml中的所有首选项,而不强制用户打开和关闭首选项活动?
我知道你可以在从SharedPreferences中检索Preference时提供默认值,但是必须在xml和code中维护默认值是愚蠢的.
我正在尝试(正确地)实现首选项屏幕,但问题是用于从xml文件中读取首选项的所有方法都已弃用(或者我只是不识别它们).开发站点上的官方示例代码(PreferenceActivity)使用不推荐使用的方法.有没有人找到一种方法来实现一个xml文件的首选项屏幕,但没有使用:addPreferencesFromResource(int)或findPreference(CharSequence)?或者在没有实现替代方案的情况下将方法标记为已弃用?
编辑:为Android版本2.1开发
是否可以以编程方式访问设置为首选项的布局?
这就是我所拥有的,一个非常简单的项目 - 概念验证
偏好活动:
package com.example;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.View;
public class PreferenceExampleActivity extends PreferenceActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ImageView v = (ImageView) findViewById(R.id.iconka);
}
}
Run Code Online (Sandbox Code Playgroud)
资源XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="settings">
<PreferenceCategory
android:title="Category Setting Name"
android:order="1"
android:key="Main">
<Preference
android:order="1"
android:title="Setting"
android:summary="Setting1"
android:layout="@layout/profile_preference_row"
android:key="profile" />
</PreferenceCategory>
</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)
首选项的自定义布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/widget_frame"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout …Run Code Online (Sandbox Code Playgroud) 我有一个PreferenceActivity,其中包括一个包含呼叫前转选项的类别.我想要的是偏好:
它可能没有任何用处,但这里有一个特殊偏好类别的片段:
<PreferenceCategory
android:title="@string/category_callforward">
<EditTextPreference
android:key="call_forward_always"
android:title="@string/call_forward_always"
android:summary="@string/call_forward_forwardto" />
</PreferenceCategory>
Run Code Online (Sandbox Code Playgroud)
编辑
如果可能的话,我想在这个方法中实现它:
// Locates the correct data from saved preferences and sets input type to numerics only
private void setCallForwardType()
{
ep1 = (EditTextPreference) findPreference("call_forward_always");
EditText et = (EditText) ep1.getEditText();
et.setKeyListener(DigitsKeyListener.getInstance());
}
Run Code Online (Sandbox Code Playgroud)
EDIT2
如果有人还在想 - 这就是我想要的偏好:

EDIT3
我现在已经搜索了几个小时,并提出了一个单词:'PreferenceGroupAdapter'.但是,我没有找到示例或教程向我展示如何使用它.建议?这甚至是正确的道路吗?
EDIT4
如果这真的不可能,我非常希望我可以实现的替代(用户友好)解决方案的建议,而不是组合的Edit-和Checkbox偏好.
我正在编写一个Android应用程序来利用抽屉布局功能.我希望能够使用抽屉布局作为整个应用程序中可用的导航工具.要使用抽屉布局,我在我的活动布局xml文件中使用以下内容:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<LinearLayout
android:id="@+id/main_content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView android:id="@+id/navigational_drawer"
android:layout_width="220dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#FFFFFF"/>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
但是,围绕PreferenceActivity包装DrawerLayout我遇到了很多困难.PreferenceActivity是一种特殊的活动,我想保留它,因为它可以帮助我在我的应用程序中保持Android首选项的外观和感觉; 话虽如此,它使用addPreferencesFromResource()调用来加载首选项,并且不使用addContentView()调用来加载XML文件; 首选项资源通常包含不喜欢包装在DrawerLayouts中的PreferenceScreen标记.
任何人都必须处理这个问题,你是如何解决的?如果您的Android应用中有PreferenceActivity,并且您在同一活动中有DrawerLayout,请分享您是如何做到的.任何帮助将非常感激.
编辑:如果我改变
<!-- The main content view -->
<LinearLayout
android:id="@+id/main_content_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
至
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
Run Code Online (Sandbox Code Playgroud)
加载PreferenceActivity时应用程序不会崩溃(因为id"list"与Preference Activity期望找到的匹配); 然而,抽屉没有显示.这几乎就像ListView"列表"下面的所有内容都被忽略了一样.
有没有办法将内容描述添加到表示 PreferenceFragment 或 PreferenceActivity 中的首选项的视图中?
基本上我需要一种方法来获取表示偏好的视图的句柄。这将允许我在视图上 setContentDescription(...)。
令人惊讶的是,这并没有在首选项 xml 文件中直接作为 xml 属性公开,因为这是一个可访问性功能,它似乎应该无处不在可以代理/生成 UI 元素。
android accessibility preferenceactivity android-preferences sharedpreferences
我有遗留代码,它使用名为"Preferences"的子类扩展了PreferenceActivity.PreferenceActivity的调用方式如下:
Intent intent = new Intent(this, Preferences.class);
this.startActivity(intent);
Run Code Online (Sandbox Code Playgroud)
OnSharedPreferenceChangeListener存在于另一个片段(不是PreferenceActivity子类)中,但需要引用PreferenceActivity以修改自定义首选项/控件的属性,类似于以下内容:
pref = (CheckBoxPreference) prefActivity.findPreference(res.getString(R.string.keyAccount));
pref.setSummary("something");
Run Code Online (Sandbox Code Playgroud)
其中"prefActivity"是对PreferenceActivity的引用.任何人都可以建议如何在创建时保存对PreferenceActivity的引用,或者在需要时定位PreferenceActivity吗?
编辑:包括严重过于简化的代码,希望有助于显示层次结构和澄清.
FragmentActivity CPActivity实例化CPFragment并按需(按下按钮)创建一个Intent来触发PreferenceActivity子类(称为"Preferences").
public class CPActivity extends FragmentActivity
{
public static CPActivity inst;
private CPFragment mFragmentCP;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
inst = this;
mFragmentCP = new CPFragment();
}
public void onSettingsButtonPressed() {
// Bring up the Preferences menu
Intent intent = new Intent(this, Preferences.class);
this.startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
CPFragment是我们的共享首选项侦听器(除其他外).在此代码中,我们要修改自定义首选项控件/条目(即,不是首选项值本身,而是首选项控件的属性,例如CheckBoxPreference).我们想在这里做,因为这是计算相关数据的地方.
public class CPFragment extends Fragment implements OnSharedPreferenceChangeListener
{
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String …Run Code Online (Sandbox Code Playgroud)