har*_*sky 9 layout android preference
我可以通过android:layout
属性为首选项设置适当的布局.举个例子
<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>
Run Code Online (Sandbox Code Playgroud)
布局在哪里
<?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>
Run Code Online (Sandbox Code Playgroud)
我可以在PreferenceActivity中获取OnCreate中的视图
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);
Run Code Online (Sandbox Code Playgroud)
但是我设定的这些事件听众并没有被解雇.我如何捕获这些事件,例如 - 单击按钮.编辑.不,它没有发动任何异常.这是更详细的代码
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();
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定您是否能够按照PreferenceActivity
上面描述的方式结合使用自定义布局。
我相信你应该:
使用PreferenceScreen
viaaddPreferencesFromResource()
并为项目实现诸如CheckBoxPreference
、DialogPreference
、 和 之MultiSelectListPreference
类的类SharedPreferences
。(例子)
或者
使用自定义布局(使用 )创建自定义Activity
(不是),并使用 手动挂钩到使用在事件侦听器(等)中编辑它们。PreferenceActivity
setContentView()
SharedPreferences
PreferenceManager.getDefaultSharedPreferences()
View.onClickListener()
SharedPreferences.Editor
希望这是有道理的。
归档时间: |
|
查看次数: |
6690 次 |
最近记录: |