如何使用另一种颜色选择器,而不preferenceActivity? [英] How can I use another Color Picker without preferenceActivity?
问题描述
我有我的共享prefereces使用颜色选择器。使用默认的ColorPicker我设法acheive我想要什么,但我注意到,没有黑色或白色的颜色。的http://www.yougli.net/android/a-photoshop-like-color-picker-for-your-android-application/
我想利用这个code,但在最后排,他给出了一个例子,在那里我可以看到它是连接到preferenced屏幕。相反,它的我用我自己的活动的按钮在哪里使用共享preferences我可以节省DATAS /值(所以它不是一个preferenceActivity,只是一个活动)。例如点击一个布局的结果:
I have a color picker which I use in sharedPrefereces. With the default colorpicker I managed to acheive what I want, but I noticed there is no black or white colors. http://www.yougli.net/android/a-photoshop-like-color-picker-for-your-android-application/ I would like to use this code but in the last rows, he shows an example, where I can see it is attached to a preferenced Screen. Instead of it I use my own activity with buttons where using shared Preferences I can save datas/values (so its not a preferenceActivity, just an Activity). For example clicking on a layout results:
OptVertexColor = (LinearLayout) findViewById(R.id.OptVC);
OptVertexColor.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LoadChartVertexColor();
ColorPickerDialog dlg = new ColorPickerDialog(settings.this,
new ColorPickerDialog.OnColorChangedListener() {
public void colorChanged(int color) {
SaveChartVertexColor("vertexcolor", color);
}
}, loadedVertexColor);
dlg.setTitle("Select new color");
dlg.show();
}
});
显示默认拾色器对话框,我还能再救一个颜色。现在,我怎么可以使用这个没有preference屏和acheive是一回事吗?我试图复制上面这个code中的code,但我coudnt弄清楚如何处理它。
The default color picker dialog appears and I can save a color. Now how can I use this without a preference screen and acheive the same thing? I tried to copy the code above to this code, but I coudnt figure out how to handle it.
public class MySettings extends PreferenceActivity implements OnPreferenceClickListener, ColorPickerDialog.OnColorChangedListener {
public boolean onPreferenceClick(Preference pref)
{
new ColorPickerDialog(this, this, DROIDS_COLOR_KEY, mPrefs.getInt(DROIDS_COLOR_KEY, DROIDS_COLOR_DEFAULT), DROIDS_COLOR_DEFAULT).show();
return true;
}
public void colorChanged(String key, int color)
{
((PreferenceScreen)this.findPreference(SETTINGS_KEY)).getEditor().putInt(key, color).commit();
}
}
感谢你在前进!
推荐答案
在自己的活动中,添加
implements ColorPickerDialog.OnColorChangedListener
到类声明。
添加到您的类主体:
public void colorChanged(String key, int color) {
//create your SharedPreferences and your SharedPreferences.Editor here
editor.putInt(key, color);
editor.commit();
}
和在点击一个按钮添加监听器:
And in a button click listener add:
new ColorPickerDialog(this, this, DROIDS_COLOR_KEY, mPrefs.getInt(DROIDS_COLOR_KEY, DROIDS_COLOR_DEFAULT), DROIDS_COLOR_DEFAULT).show();
这应该工作。让我知道,如果你我没有回答你的问题,我会看看我能做些什么。
This should work. Let me know if you I failed to answer your question, and I'll see what I can do.
这篇关于如何使用另一种颜色选择器,而不preferenceActivity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!