开发者

How to create my own Preference class

Can you please tell me how can I create my own Preference class in andorid? with my own layo开发者_运维问答ut and own click listener?

Thank you.


Well, that's about six or seven pages in a book, so it's a bit involved for a StackOverflow answer. :-)

Here is a sample project with a custom View, which is also wrapped into a custom Preference. It is only ~80 lines of code, though much of the UI smarts are bound up in the custom View class.

In a nutshell, to make a custom DialogPreference, you need to define:

  • The constructors
  • onCreateDialogView() and onBindDialogView() to create the dialog contents and populate the contents with the preference data
  • onDialogClosed() to handle when the user changes the preference
  • onGetDefaultValue() to handle your overall default if the preference is not yet set
  • onSetInitialValue() to either load in the preference value or use your default


I believe this sample is more understandable and is well depicted: http://www.codeproject.com/KB/android/seekbar_preference.aspx


As well as the other two answers, I found it's also a good idea to look at the Android source code. For example, CheckBoxPreference and DialogPreference. Neither class is very big, so if you can get your head around how they work, you should be able to implement your own.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜