如何使用 android:layout 属性实例化自定义首选项的布局
我可以通过 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>
对于布局的
<?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>
,我可以在 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);
但我设置的这些事件侦听器不会被触发。 例如,我如何捕获这些事件 - 单击按钮。 编辑。 不,它没有引发任何异常。 这是更详细的代码
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 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您是否能够按照上面描述的方式将自定义布局与
PreferenceActivity
结合使用。我相信您应该:
通过
addPreferencesFromResource()
使用PreferenceScreen
并实现CheckBoxPreference
、DialogPreference
和SharedPreferences
项的MultiSelectListPreference
。 (示例)或
创建自定义
Activity
(不是PreferenceActivity
)使用自定义布局(使用setContentView()
),并使用以下命令手动挂接到SharedPreferences
PreferenceManager.getDefaultSharedPreferences()
使用SharedPreferences.Editor
在事件监听器(View.onClickListener()
等)中编辑它们。希望这是有道理的。
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
viaaddPreferencesFromResource()
and implement classes likeCheckBoxPreference
,DialogPreference
, andMultiSelectListPreference
for theSharedPreferences
items. (example)or
Create a custom
Activity
(notPreferenceActivity
) with custom layout (usingsetContentView()
), and manually hook into theSharedPreferences
usingPreferenceManager.getDefaultSharedPreferences()
editing them in the event listeners (View.onClickListener()
, etc) usingSharedPreferences.Editor
.Hope that makes sense.
实际上我找到了另一个解决方案。您仍然可以使用首选项:
只需添加一个调用自定义布局的片段并添加它的类。
但是,您会在清单中收到警告:(您可以忽略或修复)
[res] (AndroidManifest.xml)
“YOURPACKAGE.SettingsActivity_CUSTOMLAYOUT1 不是公开的”
它仅从您的 SettingsActivity 中调用,因此您可以忽略它
,或者
如果您愿意的话要从外部调用此 Activity,只需为其创建一个自己的类并将其命名为 SettingsActivity_CUSTOMLAYOUT1.java。
代码:
[java] (SettingsActivity.java)
[xml] (pref_headers.xml)
[layout] (CUSTOMLAYOUT1.xml)
不要忘记在 Manifest 中添加。
[资源] (AndroidManifest.xml)
Actually I found for an another solution. You could still use the Preferenceability:
Simply add a Fragment which calls the Custom Layout and add it’s class.
However you'll get a warning in the Manifest: (which you can ignore or fix)
[res] (AndroidManifest.xml)
"YOURPACKAGE.SettingsActivity_CUSTOMLAYOUT1 is not public"
It's only called from your SettingsActivity so you can ignore it
or
if you want to call this Activity from outside just create an own class for it and name it SettingsActivity_CUSTOMLAYOUT1.java.
CODE:
[java] (SettingsActivity.java)
[xml] (pref_headers.xml)
[layout] (CUSTOMLAYOUT1.xml)
Dont forget to add in Manifest.
[res] (AndroidManifest.xml)