public ListPreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ListPreference, 0, 0);

    mKey = Util.checkNotNull(a.getString(R.styleable.ListPreference_key));

    // We allow the defaultValue attribute to be a string or an array of
    // strings. The reason we need multiple default values is that some
    // of them may be unsupported on a specific platform (for example,
    // continuous auto-focus). In that case the first supported value
    // in the array will be used.
    int attrDefaultValue = R.styleable.ListPreference_defaultValue;
    TypedValue tv = a.peekValue(attrDefaultValue);
    if (tv != null && tv.type == TypedValue.TYPE_REFERENCE) {
      mDefaultValues = a.getTextArray(attrDefaultValue);
    } else {
      mDefaultValues = new CharSequence[1];
      mDefaultValues[0] = a.getString(attrDefaultValue);
    }

    setEntries(a.getTextArray(R.styleable.ListPreference_entries));
    setEntryValues(a.getTextArray(R.styleable.ListPreference_entryValues));
    a.recycle();
  }
Exemplo n.º 2
0
  public MultiChoicePreference(Context context, AttributeSet attrs) {
    super(context, attrs);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MultiChoicePreference);
    mEntries = a.getTextArray(R.styleable.MultiChoicePreference_entries);
    mEntryKeys = a.getTextArray(R.styleable.MultiChoicePreference_entryKeys);
    mInitialValues = a.getTextArray(R.styleable.MultiChoicePreference_initialValues);
    a.recycle();

    loadPersistedValues();
  }
 private void setup(Context context, AttributeSet attrs) {
   setDialogLayoutResource(R.layout.slider_preference_dialog);
   TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SliderPreference);
   try {
     setSummary(a.getTextArray(R.styleable.SliderPreference_android_summary));
   } catch (Exception e) {
     // Do nothing
   }
   a.recycle();
 }
  @Override
  protected Object onGetDefaultValue(TypedArray a, int index) {
    final CharSequence[] defaultValues = a.getTextArray(index);
    final int valueCount = defaultValues.length;
    final Set<String> result = new HashSet<String>();

    for (int i = 0; i < valueCount; i++) {
      result.add(defaultValues[i].toString());
    }

    return result;
  }
  /** Parse services.xml file to find our available email services */
  private static Map<String, EmailServiceInfo> getServiceMap(final Context context) {
    synchronized (sServiceMapLock) {
      /**
       * We cache localized strings here, so make sure to regenerate the service map if the locale
       * changes
       */
      if (sServiceMap == null) {
        sOldConfiguration.setTo(context.getResources().getConfiguration());
      }

      final int delta = sOldConfiguration.updateFrom(context.getResources().getConfiguration());

      if (sServiceMap != null
          && !Configuration.needNewResources(delta, ActivityInfo.CONFIG_LOCALE)) {
        return sServiceMap;
      }

      final ImmutableMap.Builder<String, EmailServiceInfo> builder = ImmutableMap.builder();

      try {
        final Resources res = context.getResources();
        final XmlResourceParser xml = res.getXml(R.xml.services);
        int xmlEventType;
        // walk through senders.xml file.
        while ((xmlEventType = xml.next()) != XmlResourceParser.END_DOCUMENT) {
          if (xmlEventType == XmlResourceParser.START_TAG && "emailservice".equals(xml.getName())) {
            final EmailServiceInfo info = new EmailServiceInfo();
            final TypedArray ta = res.obtainAttributes(xml, R.styleable.EmailServiceInfo);
            info.protocol = ta.getString(R.styleable.EmailServiceInfo_protocol);
            info.accountType = ta.getString(R.styleable.EmailServiceInfo_accountType);
            info.name = ta.getString(R.styleable.EmailServiceInfo_name);
            info.hide = ta.getBoolean(R.styleable.EmailServiceInfo_hide, false);
            final String klass = ta.getString(R.styleable.EmailServiceInfo_serviceClass);
            info.intentAction = ta.getString(R.styleable.EmailServiceInfo_intent);
            info.intentPackage = ta.getString(R.styleable.EmailServiceInfo_intentPackage);
            info.defaultSsl = ta.getBoolean(R.styleable.EmailServiceInfo_defaultSsl, false);
            info.port = ta.getInteger(R.styleable.EmailServiceInfo_port, 0);
            info.portSsl = ta.getInteger(R.styleable.EmailServiceInfo_portSsl, 0);
            info.offerTls = ta.getBoolean(R.styleable.EmailServiceInfo_offerTls, false);
            info.offerCerts = ta.getBoolean(R.styleable.EmailServiceInfo_offerCerts, false);
            info.offerLocalDeletes =
                ta.getBoolean(R.styleable.EmailServiceInfo_offerLocalDeletes, false);
            info.defaultLocalDeletes =
                ta.getInteger(
                    R.styleable.EmailServiceInfo_defaultLocalDeletes,
                    Account.DELETE_POLICY_ON_DELETE);
            info.offerPrefix = ta.getBoolean(R.styleable.EmailServiceInfo_offerPrefix, false);
            info.usesSmtp = ta.getBoolean(R.styleable.EmailServiceInfo_usesSmtp, false);
            info.usesAutodiscover =
                ta.getBoolean(R.styleable.EmailServiceInfo_usesAutodiscover, false);
            info.offerLookback = ta.getBoolean(R.styleable.EmailServiceInfo_offerLookback, false);
            info.defaultLookback =
                ta.getInteger(
                    R.styleable.EmailServiceInfo_defaultLookback, SyncWindow.SYNC_WINDOW_3_DAYS);
            info.syncChanges = ta.getBoolean(R.styleable.EmailServiceInfo_syncChanges, false);
            info.syncContacts = ta.getBoolean(R.styleable.EmailServiceInfo_syncContacts, false);
            info.syncCalendar = ta.getBoolean(R.styleable.EmailServiceInfo_syncCalendar, false);
            info.offerAttachmentPreload =
                ta.getBoolean(R.styleable.EmailServiceInfo_offerAttachmentPreload, false);
            info.syncIntervalStrings =
                ta.getTextArray(R.styleable.EmailServiceInfo_syncIntervalStrings);
            info.syncIntervals = ta.getTextArray(R.styleable.EmailServiceInfo_syncIntervals);
            info.defaultSyncInterval =
                ta.getInteger(R.styleable.EmailServiceInfo_defaultSyncInterval, 15);
            info.inferPrefix = ta.getString(R.styleable.EmailServiceInfo_inferPrefix);
            info.offerLoadMore = ta.getBoolean(R.styleable.EmailServiceInfo_offerLoadMore, false);
            info.offerMoveTo = ta.getBoolean(R.styleable.EmailServiceInfo_offerMoveTo, false);
            info.requiresSetup = ta.getBoolean(R.styleable.EmailServiceInfo_requiresSetup, false);

            // Must have either "class" (local) or "intent" (remote)
            if (klass != null) {
              try {
                // noinspection unchecked
                info.klass = (Class<? extends Service>) Class.forName(klass);
              } catch (ClassNotFoundException e) {
                throw new IllegalStateException("Class not found in service descriptor: " + klass);
              }
            }
            if (info.klass == null && info.intentAction == null) {
              throw new IllegalStateException(
                  "No class or intent action specified in service descriptor");
            }
            if (info.klass != null && info.intentAction != null) {
              throw new IllegalStateException(
                  "Both class and intent action specified in service descriptor");
            }
            builder.put(info.protocol, info);
          }
        }
      } catch (XmlPullParserException e) {
        // ignore
      } catch (IOException e) {
        // ignore
      }
      sServiceMap = builder.build();
      return sServiceMap;
    }
  }
  /**
   * 介绍TypedArray使用方法 获取 attr中formate 十种类型属性值
   * float,integer,boolean,fraction,string,dimension,color,reference,enum,flag
   *
   * <p>
   *
   * @param context 上下文环境
   * @param attrs 属性集合
   */
  private void printAttributes(Context context, AttributeSet attrs) {
    TypedArray typedArray =
        context.obtainStyledAttributes(attrs, R.styleable.CustomTextViewStyle, 0, 0);
    System.out.println(
        "typedArray.getChangingConfigurations() = "
            + Integer.toHexString(typedArray.getChangingConfigurations()));
    float float_value = typedArray.getFloat(R.styleable.CustomTextViewStyle_float_value, 0f);
    int integer_value = typedArray.getInteger(R.styleable.CustomTextViewStyle_integer_value, 0);
    boolean boolean_value =
        typedArray.getBoolean(R.styleable.CustomTextViewStyle_boolean_value, false);

    // public float getFraction (int index, int base, int pbase, float defValue)
    // 如果值为10% 则 fraction_value=10%*base
    // 如果值格式为10%p,则fraction_value=10%*pbase
    float fraction_value =
        typedArray.getFraction(R.styleable.CustomTextViewStyle_fraction_value, 1, 1, 0);
    String string_value = typedArray.getString(R.styleable.CustomTextViewStyle_string_value);

    // 获取像素值,浮点数  eg:27.5625
    float dimension_value_float =
        typedArray.getDimension(R.styleable.CustomTextViewStyle_dimension_value, 0f);
    // 将取得浮点像素值四舍五入 eg:28
    int dimension_value =
        typedArray.getDimensionPixelSize(R.styleable.CustomTextViewStyle_dimension_value, 0);
    // 将取得浮点像素值直接截取整数部分 eg:27
    int dimension_value_offset_ =
        typedArray.getDimensionPixelOffset(R.styleable.CustomTextViewStyle_dimension_value, 0);

    int color_value = typedArray.getColor(R.styleable.CustomTextViewStyle_color_value, 0);
    int reference_drawable_value =
        typedArray.getResourceId(R.styleable.CustomTextViewStyle_reference_drawable_value, 0);
    int reference_array_value =
        typedArray.getResourceId(R.styleable.CustomTextViewStyle_reference_array_value, 0);
    int enum_value = typedArray.getInt(R.styleable.CustomTextViewStyle_enum_value, -1);
    int flag_value = typedArray.getInt(R.styleable.CustomTextViewStyle_flag_value, -1);

    System.out.println(
        "float_value = ["
            + float_value
            + "], integer_value = ["
            + integer_value
            + "], "
            + "boolean_value = ["
            + boolean_value
            + "], fraction_value = ["
            + fraction_value
            + "], string_value = ["
            + string_value
            + "], dimension_value = ["
            + dimension_value
            + "], color_value = ["
            + color_value
            + "], "
            + "reference_drawable_value ="
            + " [0x"
            + Integer.toHexString(reference_drawable_value)
            + "], enum_value = ["
            + enum_value
            + "], "
            + "flag_value1 = ["
            + flag_value
            + "]");

    // 后期数据处理,设置左边图片
    Drawable drawable;
    drawable = typedArray.getDrawable(R.styleable.CustomTextViewStyle_reference_drawable_value);
    // or
    drawable = context.getDrawable(reference_drawable_value);
    drawable.setBounds(new Rect(0, 0, 50, 50));
    setCompoundDrawables(drawable, null, null, null);
    // 设置文字是否大写,斜体
    if (flag_value >= 0) {
      Typeface typeface = getTypeface();
      setTypeface(Typeface.defaultFromStyle(flag_value));
    }

    // 其他方法getTextArray
    CharSequence[] arrays;
    arrays = typedArray.getTextArray(R.styleable.CustomTextViewStyle_reference_array_value);
    // or
    arrays = context.getResources().getTextArray(reference_array_value);
    for (int i = 0; i < arrays.length; i++) {
      System.out.println("arrays[" + i + "] = " + arrays[i]);
    }

    // 遍历TypedArray
    for (int i = 0, m = typedArray.getIndexCount(); i < m; i++) {
      System.out.println(
          "typedArray"
              + i
              + " type= "
              + typedArray.getType(i)
              + " value="
              + typedArray.getString(i));
    }

    int textsize = typedArray.getDimensionPixelSize(R.styleable.CustomTextViewStyle_text_size, 10);
    int textcolor = typedArray.getColor(R.styleable.CustomTextViewStyle_text_color, Color.BLACK);
    String text = typedArray.getString(R.styleable.CustomTextViewStyle_text_content);
    int padding = typedArray.getDimensionPixelSize(R.styleable.CustomTextViewStyle_padding, 0);

    setTextColor(textcolor);
    setTextSize(textsize);
    setText(text);
    setPadding(padding, padding, padding, padding);

    typedArray.recycle();
  }
  public MaterialCalendarView(Context context, AttributeSet attrs) {
    super(context, attrs);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      // If we're on good Android versions, turn off clipping for cool effects
      setClipToPadding(false);
      setClipChildren(false);
    } else {
      // Old Android does not like _not_ clipping view pagers, we need to clip
      setClipChildren(true);
      setClipToPadding(true);
    }

    buttonPast = new DirectionButton(getContext());
    title = new TextView(getContext());
    buttonFuture = new DirectionButton(getContext());
    pager = new ViewPager(getContext());

    setupChildren();

    title.setOnClickListener(onClickListener);
    buttonPast.setOnClickListener(onClickListener);
    buttonFuture.setOnClickListener(onClickListener);

    titleChanger = new TitleChanger(title);
    titleChanger.setTitleFormatter(DEFAULT_TITLE_FORMATTER);
    adapter = new MonthPagerAdapter();
    adapter.setTitleFormatter(DEFAULT_TITLE_FORMATTER);
    pager.setAdapter(adapter);
    pager.setOnPageChangeListener(pageChangeListener);
    pager.setPageTransformer(
        false,
        new ViewPager.PageTransformer() {
          @Override
          public void transformPage(View page, float position) {
            position = (float) Math.sqrt(1 - Math.abs(position));
            page.setAlpha(position);
          }
        });

    adapter.setCallbacks(monthViewCallbacks);

    TypedArray a =
        context.getTheme().obtainStyledAttributes(attrs, R.styleable.MaterialCalendarView, 0, 0);
    try {

      int tileSize = a.getDimensionPixelSize(R.styleable.MaterialCalendarView_mcv_tileSize, -1);
      if (tileSize > 0) {
        setTileSize(tileSize);
      }

      setArrowColor(a.getColor(R.styleable.MaterialCalendarView_mcv_arrowColor, Color.BLACK));
      Drawable leftMask = a.getDrawable(R.styleable.MaterialCalendarView_mcv_leftArrowMask);
      if (leftMask == null) {
        leftMask = getResources().getDrawable(R.drawable.mcv_action_previous);
      }
      setLeftArrowMask(leftMask);
      Drawable rightMask = a.getDrawable(R.styleable.MaterialCalendarView_mcv_rightArrowMask);
      if (rightMask == null) {
        rightMask = getResources().getDrawable(R.drawable.mcv_action_next);
      }
      setRightArrowMask(rightMask);

      setSelectionColor(
          a.getColor(
              R.styleable.MaterialCalendarView_mcv_selectionColor, getThemeAccentColor(context)));

      CharSequence[] array = a.getTextArray(R.styleable.MaterialCalendarView_mcv_weekDayLabels);
      if (array != null) {
        setWeekDayFormatter(new ArrayWeekDayFormatter(array));
      }

      array = a.getTextArray(R.styleable.MaterialCalendarView_mcv_monthLabels);
      if (array != null) {
        setTitleFormatter(new MonthArrayTitleFormatter(array));
      }

      setHeaderTextAppearance(
          a.getResourceId(
              R.styleable.MaterialCalendarView_mcv_headerTextAppearance,
              R.style.TextAppearance_MaterialCalendarWidget_Header));
      setWeekDayTextAppearance(
          a.getResourceId(
              R.styleable.MaterialCalendarView_mcv_weekDayTextAppearance,
              R.style.TextAppearance_MaterialCalendarWidget_WeekDay));
      setDateTextAppearance(
          a.getResourceId(
              R.styleable.MaterialCalendarView_mcv_dateTextAppearance,
              R.style.TextAppearance_MaterialCalendarWidget_Date));
      setShowOtherDates(a.getBoolean(R.styleable.MaterialCalendarView_mcv_showOtherDates, false));

      int firstDayOfWeek = a.getInt(R.styleable.MaterialCalendarView_mcv_firstDayOfWeek, -1);
      if (firstDayOfWeek < 0) {
        firstDayOfWeek = CalendarUtils.getInstance().getFirstDayOfWeek();
      }
      setFirstDayOfWeek(firstDayOfWeek);
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      a.recycle();
    }

    currentMonth = CalendarDay.today();
    setCurrentDate(currentMonth);

    if (isInEditMode()) {
      removeView(pager);
      MonthView monthView = new MonthView(context, currentMonth, getFirstDayOfWeek());
      monthView.setSelectionColor(getSelectionColor());
      monthView.setDateTextAppearance(adapter.getDateTextAppearance());
      monthView.setWeekDayTextAppearance(adapter.getWeekDayTextAppearance());
      monthView.setShowOtherDates(getShowOtherDates());
      addView(monthView, new LayoutParams(MonthView.DEFAULT_MONTH_TILE_HEIGHT));
    }
  }
  private void readAttrs(final Context context, final AttributeSet attrs, final int defStyle) {
    final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GaugeView, defStyle, 0);
    mShowOuterShadow = a.getBoolean(R.styleable.GaugeView_showOuterShadow, SHOW_OUTER_SHADOW);
    mShowOuterBorder = a.getBoolean(R.styleable.GaugeView_showOuterBorder, SHOW_OUTER_BORDER);
    mShowOuterRim = a.getBoolean(R.styleable.GaugeView_showOuterRim, SHOW_OUTER_RIM);
    mShowInnerRim = a.getBoolean(R.styleable.GaugeView_showInnerRim, SHOW_INNER_RIM);
    mShowNeedle = a.getBoolean(R.styleable.GaugeView_showNeedle, SHOW_NEEDLE);
    mShowScale = a.getBoolean(R.styleable.GaugeView_showScale, SHOW_SCALE);
    mShowRanges = a.getBoolean(R.styleable.GaugeView_showRanges, SHOW_RANGES);
    mShowText = a.getBoolean(R.styleable.GaugeView_showText, SHOW_TEXT);

    mOuterShadowWidth =
        mShowOuterShadow
            ? a.getFloat(R.styleable.GaugeView_outerShadowWidth, OUTER_SHADOW_WIDTH)
            : 0.0f;
    mOuterBorderWidth =
        mShowOuterBorder
            ? a.getFloat(R.styleable.GaugeView_outerBorderWidth, OUTER_BORDER_WIDTH)
            : 0.0f;
    mOuterRimWidth =
        mShowOuterRim ? a.getFloat(R.styleable.GaugeView_outerRimWidth, OUTER_RIM_WIDTH) : 0.0f;
    mInnerRimWidth =
        mShowInnerRim ? a.getFloat(R.styleable.GaugeView_innerRimWidth, INNER_RIM_WIDTH) : 0.0f;
    mInnerRimBorderWidth =
        mShowInnerRim
            ? a.getFloat(R.styleable.GaugeView_innerRimBorderWidth, INNER_RIM_BORDER_WIDTH)
            : 0.0f;

    mNeedleWidth = a.getFloat(R.styleable.GaugeView_needleWidth, NEEDLE_WIDTH);
    mNeedleHeight = a.getFloat(R.styleable.GaugeView_needleHeight, NEEDLE_HEIGHT);

    mScalePosition =
        (mShowScale || mShowRanges)
            ? a.getFloat(R.styleable.GaugeView_scalePosition, SCALE_POSITION)
            : 0.0f;
    mScaleStartValue = a.getFloat(R.styleable.GaugeView_scaleStartValue, SCALE_START_VALUE);
    mScaleEndValue = a.getFloat(R.styleable.GaugeView_scaleEndValue, SCALE_END_VALUE);
    mScaleStartAngle = a.getFloat(R.styleable.GaugeView_scaleStartAngle, SCALE_START_ANGLE);
    mScaleEndAngle = a.getFloat(R.styleable.GaugeView_scaleEndAngle, 360.0f - mScaleStartAngle);

    mDivisions = a.getInteger(R.styleable.GaugeView_divisions, SCALE_DIVISIONS);
    mSubdivisions = a.getInteger(R.styleable.GaugeView_subdivisions, SCALE_SUBDIVISIONS);

    if (mShowRanges) {
      mTextShadowColor = a.getColor(R.styleable.GaugeView_textShadowColor, TEXT_SHADOW_COLOR);

      final CharSequence[] rangeValues = a.getTextArray(R.styleable.GaugeView_rangeValues);
      final CharSequence[] rangeColors = a.getTextArray(R.styleable.GaugeView_rangeColors);
      readRanges(rangeValues, rangeColors);
    }

    if (mShowText) {
      final int textValueId = a.getResourceId(R.styleable.GaugeView_textValue, 0);
      final String textValue = a.getString(R.styleable.GaugeView_textValue);
      mTextValue =
          (0 < textValueId) ? context.getString(textValueId) : (null != textValue) ? textValue : "";

      final int textUnitId = a.getResourceId(R.styleable.GaugeView_textUnit, 0);
      final String textUnit = a.getString(R.styleable.GaugeView_textUnit);
      mTextUnit =
          (0 < textUnitId) ? context.getString(textUnitId) : (null != textUnit) ? textUnit : "";
      mTextValueColor = a.getColor(R.styleable.GaugeView_textValueColor, TEXT_VALUE_COLOR);
      mTextUnitColor = a.getColor(R.styleable.GaugeView_textUnitColor, TEXT_UNIT_COLOR);
      mTextShadowColor = a.getColor(R.styleable.GaugeView_textShadowColor, TEXT_SHADOW_COLOR);

      mTextValueSize = a.getFloat(R.styleable.GaugeView_textValueSize, TEXT_VALUE_SIZE);
      mTextUnitSize = a.getFloat(R.styleable.GaugeView_textUnitSize, TEXT_UNIT_SIZE);
    }

    a.recycle();
  }