本文整理汇总了Java中android.util.Property.getType方法的典型用法代码示例。如果您正苦于以下问题:Java Property.getType方法的具体用法?Java Property.getType怎么用?Java Property.getType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.util.Property
的用法示例。
在下文中一共展示了Property.getType方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getProperty
import android.util.Property; //导入方法依赖的package包/类
/**
* Retrieves the value stored in {@link android.content.SharedPreferences} for a
* given {@link android.util.Property} associated with a VolumePanel.
* @return The given value, {@code defVal} if none was set, or null is the
* value could not be retrieved.
* @throws ClassCastException If a type error occurred between SP and Property.
*/
@SuppressWarnings("unchecked")
public <T, E> E getProperty(Class<T> clazz, Property<T, E> property, E defVal)
throws ClassCastException {
Class<E> type = property.getType();
String name = getName(clazz, property);
// Handle all types supported by SharedPreferences.
if (type.equals(Integer.TYPE) || type.equals(Integer.class))
return (E) Integer.valueOf(mPreferences.getInt(name, (Integer) defVal));
else if (type.equals(String.class) || type.equals(CharSequence.class))
return (E) mPreferences.getString(name, ((defVal == null) ? (String) defVal : defVal.toString()));
else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class))
return (E) Boolean.valueOf(mPreferences.getBoolean(name, (Boolean) defVal));
else if (type.equals(Long.TYPE) || type.equals(Long.class))
return (E) Long.valueOf(mPreferences.getLong(name, (Long) defVal));
else if (type.equals(Float.TYPE) || type.equals(Float.class))
return (E) Float.valueOf(mPreferences.getFloat(name, (Float) defVal));
else if (type.getClass().isAssignableFrom(Set.class))
return (E) mPreferences.getStringSet(name, (Set<String>) defVal);
return defVal;
}
示例2: setProperty
import android.util.Property; //导入方法依赖的package包/类
/**
* Sets the value for a given {@link android.util.Property}. Uses
* {@link android.content.SharedPreferences.Editor#apply()} to store asynchronously.
* @return True if the value was successfully applied, false if else.
* @throws ClassCastException If a type error occurred between SP and Property.
*/
@SuppressWarnings("unchecked")
public <T, E> boolean setProperty(Class<T> clazz, Property<T, E> property, E val)
throws ClassCastException {
Class<E> type = property.getType();
String name = getName(clazz, property);
SharedPreferences.Editor editor = edit();
// Handle all types supported by SharedPreferences.
if (type.equals(Integer.TYPE) || type.equals(Integer.class))
editor.putInt(name, (Integer) val);
else if (type.equals(String.class) || type.equals(CharSequence.class))
editor.putString(name, val.toString());
else if (type.equals(Boolean.TYPE) || type.equals(Boolean.class))
editor.putBoolean(name, (Boolean) val);
else if (type.equals(Long.TYPE) || type.equals(Long.class))
editor.putLong(name, (Long) val);
else if (type.equals(Float.TYPE) || type.equals(Float.class))
editor.putFloat(name, (Float) val);
else if (type.getClass().isAssignableFrom(Set.class))
editor.putStringSet(name, (Set<String>) val);
else
editor = null;
if (null == editor) return false;
editor.apply();
return true;
}