Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2028364
  • 博文数量: 413
  • 博客积分: 10926
  • 博客等级: 上将
  • 技术积分: 3862
  • 用 户 组: 普通用户
  • 注册时间: 2006-01-09 18:14
文章分类

全部博文(413)

文章存档

2015年(5)

2014年(1)

2013年(5)

2012年(6)

2011年(138)

2010年(85)

2009年(42)

2008年(46)

2007年(26)

2006年(59)

分类: 嵌入式

2010-09-10 10:46:31

  1. Documents
    • Preference Package
      http://developer.android.com/reference/android/preference/package-summary.html
    • Preference xml File Sample
      http://developer.android.com/resources/samples/ApiDemos/res/xml/preferences.html
    • xxx
  2. Preference Class
    • PreferenceActivity
      In general, we use preference in the subclass of PreferenceActivity, and call addPreferencesFromResource to load layout file. If you want to use preference in the subclass of Activity, you must call PreferenceManager.setDefaultValues to load layout file.
    • PreferenceManager
      If you want to create preference in code, or if you want to use preference in the subclass of Activity, or if you want to get the object of SharedPreferences, you can use this class.
    • PreferenceGroup
      This is the base class of PreferenceScreen and PreferenceCategory, it is rarely used directly.
      • PreferenceScreen
        Represent a preference window/screen
      • PreferenceCategory
        Represent a group in the window/screen
    • Preference
      It is the base class of all preference elements. If you want to customize preference's behavior (such as to pop up yourown screen when the preference is clicked), you can use this class, and register the listener of type Preference.OnPreferenceClickListener, then decide what to do in the methods of Preference.OnPreferenceClickListener.
      • DialogPreference
        Must create a derived class of DialogPreference, and then use the derived clas, as
      • CheckBoxPreference
      • EditTextPreference
      • ListPreference
        1. On Android 1.6 and previous version, ListPreference has no icon, if you want to show icon on the right of the preference as that on Android 2.0 and later, just replace ListPreference with PreferenceScreen, and process the proper events.
      • RingtonePreference
    • xxx
  3. Usage
    • Define preference layout in res/xml/ (you can also create preference elements in your code)
    • Define subclass of PreferenceActivity, in the method onCreate, call addPreferencesFromResource(R.xml.xxx) to load layout resource file
    • Get preference element handle by invoking the method findPreference
  4. SharedPreferences
    You can call getSharedPreferences() in subclass of Context to get shared preference handle, then to get/set preferences.
  5. Customize Preference
    • Define layout
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:stretchColumns="1">

         
                          android:gravity="right" />
                          android:singleLine="true"
                  android:password="true"
                  android:gravity="left" />
         

         
         
                          android:gravity="right" />
                          android:singleLine="true"
                  android:password="true"
                  android:gravity="left" />
         


    • Define class
      public class ChangePasswdDialogPreferenceextends DialogPreference
      {
          private EditText mNewPasswdView;
          private EditText mNewPasswdConfirmView;
         
          public PhoneRebindDialogPreference(Context context, AttributeSet attrs)
          {
             super(context, attrs);
          }
         
         
          public PhoneRebindDialogPreference(Context context, AttributeSet attrs, int defStyle)
          {
             super(context, attrs, defStyle);
          }
         

          @Override
          protected View onCreateDialogView()
          {
              View view = super.onCreateDialogView();
             
              mNewPasswdView = (EditText)view.findViewById(R.id.new_password);
              mNewPasswdConfirmView = (EditText)view.findViewById(R.id.new_passwd_confirm);
             
              return view;
          }
         
         
          @Override
          protected void showDialog(Bundle state)
          {
              super.showDialog(state);
             
              Button button;
             
              button = ((AlertDialog)getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
              button.setOnClickListener(new View.OnClickListener()
              {
                  public void onClick(View v)
                  {
                      xxxx
                  }
              });
          }
         
        
          //Save & restore state
          @Override
          protected Parcelable onSaveInstanceState()
          {
              final Parcelable superState = super.onSaveInstanceState();
              if (isPersistent())
              {
                  // No need to save instance state since it's persistent
                  return superState;
              }
             
              final SavedState myState = new SavedState(superState);
              myState.mNewPasswd = mNewPasswdView.getText().toString();
              myState.mNewPasswdConfirm = mNewPasswdConfirmView.getText().toString();
              return myState;
          }

         
          @Override
          protected void onRestoreInstanceState(Parcelable state)
          {
              if (state == null || !state.getClass().equals(SavedState.class))
              {
                  // Didn't save state for us in onSaveInstanceState
                  super.onRestoreInstanceState(state);
                  return;
              }
              
              SavedState myState = (SavedState) state;
              super.onRestoreInstanceState(myState.getSuperState());

              mNewPasswd.setText(myState.mNewPasswd);
              mNewPasswdConfirmView.setText(myState.mNewPasswdConfirm);
          }
         
         
          private static class SavedState extends BaseSavedState
          {
              String mNewPasswd;
              String mNewPasswdConfirm;
             
              public SavedState(Parcel source)
              {
                  super(source);
                 
                  mNewPasswd = source.readString();
                  mNewPasswdConfirm = source.readString();
              }

              @Override
              public void writeToParcel(Parcel dest, int flags)
              {
                  super.writeToParcel(dest, flags);
                 
                  dest.writeString(mNewPasswd);
                  dest.writeString(mNewPasswdConfirm );
              }

              public SavedState(Parcelable superState)
              {
                  super(superState);
              }

              public static final Parcelable.Creator CREATOR =
                      new Parcelable.Creator()
                      {
                          public SavedState createFromParcel(Parcel in)
                          {
                              return new SavedState(in);
                          }

                          public SavedState[] newArray(int size)
                          {
                              return new SavedState[size];
                          }
                      };
          }
    • Use customized preference
                      android:key="change_contents"
                      android:title="@string/pref_title_change_contents"
                      android:dialogLayout="@layout/my_dialog"
                      android:summary="" />
    • xx
  6. xxx
阅读(1361) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~