我可以通过Android的设置preference适当布局:布局属性。举一个例子
< preference安卓键=摩的Android:标题=@字符串/摩的Android:布局=@布局/ friction_fragment机器人:shouldDisableView =真正的机器人:设置defaultValue =30机器人:启用=真正的机器人:选择=真正的机器人:总结=咬摩擦>< / preference>
在这里布局
< XML版本=1.0编码=UTF-8&GT?;
<的LinearLayout
的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT>
< TextView的机器人:ID =@ + ID / textView1机器人:layout_width =WRAP_CONTENT机器人:文本=@字符串/摩的Android:textAppearance =?机器人:ATTR / textAppearanceLarge机器人:layout_height =WRAP_CONTENT机器人:layout_gravity =center_horizontal>< / TextView的>
<搜索栏安卓layout_height =WRAP_CONTENT机器人:layout_width =FILL_PARENT机器人:ID =@ + ID / sbFriction>< /搜索栏>
< TextView的Android版本:文本=@字符串/ friction_little机器人:ID =@ + ID / txtSummary机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< / TextView的>
<按钮的android:文本=按钮机器人:ID =@ + ID / btnFriction机器人:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT>< /按钮>
< / LinearLayout中>
我可以得到意见的OnCreate在preferenceActivity
preference FRIC =(preference)this.find preference(摩擦);
视图V = fric.getView(NULL,NULL);
搜索栏sbFriction =(搜索栏)v.findViewById(R.id.sbFriction);
sbFriction.setOnSeekBarChangeListener(本);
按钮btnFric =(按钮)v.findViewById(R.id.btnFriction);
btnFric.setOnClickListener(m_onClick);
但这些事件的听众,我已经设置,不会被触发。 我如何能抓住这些事件,例如 - 点选按钮。 编辑。 不过,这并没有引发任何异常。 下面是更详细的code
公共类SettingsActivity扩展preferenceActivity实现在preferenceChangeListener,OnSeekBarChangeListener
{
私人TextView的m_txtSummary;
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
加preferencesFromResource(R.xml preferences。);
名单preference难度=(名单preference)this.find preference(看病难);
difficulty.setSummary(difficulty.getEntry());
difficulty.setOn preferenceChangeListener(本);
preference FRIC =(preference)this.find preference(摩擦);
视图V = fric.getView(NULL,NULL);
搜索栏sbFriction =(搜索栏)v.findViewById(R.id.sbFriction);
sbFriction.setOnSeekBarChangeListener(本);
按钮btnFric =(按钮)v.findViewById(R.id.btnFriction);
btnFric.setOnClickListener(m_onClick);
m_txtSummary =(TextView中)v.findViewById(R.id.txtSummary);
fric.setSummary(fric.toString());
fric.setOn preferenceChangeListener(本);
复选框preference音乐=(复选框preference)this.find preference(音乐);
music.setOn preferenceChangeListener(本);
}
私人OnClickListener m_onClick =新OnClickListener(){
@覆盖
公共无效的onClick(视图v){
// TODO自动生成方法存根
v.getId();
}
};
@覆盖
在preferenceChange公共布尔(preference preference,对象为newValue){
如果(为newValue的instanceof布尔)
返回true;
preference.setSummary(newValue.toString());
返回true;
}
@覆盖
公共无效onProgressChanged(搜索栏V,诠释nProgress,布尔ARG2){
// TODO自动生成方法存根
m_txtSummary.append(+ nProgress);
m_txtSummary.invalidate();
}
@覆盖
公共无效onStartTrackingTouch(搜索栏为arg0){
// TODO自动生成方法存根
}
@覆盖
公共无效onStopTrackingTouch(搜索栏为arg0){
// TODO自动生成方法存根
// notifyChanged();
}
}
解决方案
我不知道,你可以使用自定义布局与 preferenceActivity 在你上面描述的方式。
我相信你应该:
使用一个 preferenceScreen
通过添加preferencesFromResource()
键,实现诸如复选框preference ,对话preference
和 MultiSelectList preference
为共享preferences
项目。 (例如)
或
创建一个自定义的活动
(而不是 preferenceActivity
)的自定义布局(使用的setContentView()
),并手工勾入共享preferences
使用 preferenceManager.getDefaultShared preferences()
编辑他们在事件侦听器( View.onClickListener()
等),使用共享preferences.Editor
。
希望是有道理的。
I can set appropriate layout for preference through android:layout attribute. For an example
<Preference android:key="friction" android:title="@string/friction" android:layout="@layout/friction_fragment" android:shouldDisableView="true" android:defaultValue="30" android:enabled="true" android:selectable="true" android:summary="Bite friction"></Preference>
where layout is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:text="@string/friction" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"></TextView>
<SeekBar android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/sbFriction"></SeekBar>
<TextView android:text="@string/friction_little" android:id="@+id/txtSummary" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<Button android:text="Button" android:id="@+id/btnFriction" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
I can get views in OnCreate in PreferenceActivity
Preference fric = (Preference)this.findPreference("friction");
View v = fric.getView(null, null);
SeekBar sbFriction = (SeekBar)v.findViewById(R.id.sbFriction);
sbFriction.setOnSeekBarChangeListener(this);
Button btnFric = (Button) v.findViewById(R.id.btnFriction);
btnFric.setOnClickListener(m_onClick);
but these events listeners, that I have set, are not fired. How I can catch these events, for example - click from button. Edit. No, It did not fire any exception. Here is more detailed code
public class SettingsActivity extends PreferenceActivity implements OnPreferenceChangeListener, OnSeekBarChangeListener
{
private TextView m_txtSummary;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
ListPreference difficulty = (ListPreference)this.findPreference("difficulty");
difficulty.setSummary(difficulty.getEntry());
difficulty.setOnPreferenceChangeListener(this);
Preference fric = (Preference)this.findPreference("friction");
View v = fric.getView(null, null);
SeekBar sbFriction = (SeekBar)v.findViewById(R.id.sbFriction);
sbFriction.setOnSeekBarChangeListener(this);
Button btnFric = (Button) v.findViewById(R.id.btnFriction);
btnFric.setOnClickListener(m_onClick);
m_txtSummary = (TextView)v.findViewById(R.id.txtSummary);
fric.setSummary(fric.toString());
fric.setOnPreferenceChangeListener(this);
CheckBoxPreference music = (CheckBoxPreference)this.findPreference("music");
music.setOnPreferenceChangeListener(this);
}
private OnClickListener m_onClick = new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
v.getId();
}
};
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if(newValue instanceof Boolean)
return true;
preference.setSummary(newValue.toString());
return true;
}
@Override
public void onProgressChanged(SeekBar v, int nProgress, boolean arg2) {
// TODO Auto-generated method stub
m_txtSummary.append(" " + nProgress);
m_txtSummary.invalidate();
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
//notifyChanged();
}
}
解决方案
I'm not sure you are able to use a custom layout in conjunction with a PreferenceActivity
in the way that you describe above.
I believe you should either:
Use a PreferenceScreen
via addPreferencesFromResource()
and implement classes like CheckBoxPreference
, DialogPreference
, and MultiSelectListPreference
for the SharedPreferences
items. (example)
or
Create a custom Activity
(not PreferenceActivity
) with custom layout (using setContentView()
), and manually hook into the SharedPreferences
using PreferenceManager.getDefaultSharedPreferences()
editing them in the event listeners (View.onClickListener()
, etc) using SharedPreferences.Editor
.
Hope that makes sense.