1. SharedPreference 无法写入值
先看下代码:
/**写入*/public void storePreference(boolean value) {if(preference == null) {preference = getPreferences(Context.MODE_PRIVATE);}Log.d("Write SharedPreferences", "FLAG: " + value);preference.edit().putBoolean("FLAG", value);preference.edit().commit();}
咋看好像是正确的.. 实际上这样写是不对的。 正确的写法:
/**写入*/public void storePreference(boolean value) {if(preference == null) {preference = getPreferences(Context.MODE_PRIVATE);}Log.d("Write SharedPreferences", "FLAG: " + value);preference.edit().putBoolean("FLAG", value).commit();}
或者:
/**写入*/public void storePreference(boolean value) {if(preference == null) {preference = getPreferences(Context.MODE_PRIVATE);}Log.d("Write SharedPreferences", "FLAG: " + value);SharedPreferences.Editor editor = preference.edit();editor.putBoolean("FLAG", value);editor.commit();}
2. AlarmManager

3.getSharedPreferences 与 getPreferences 的区别。
getSharedPreferences 是Context类中的方法, 可以指定file name 以及 mode。
getPreferences是Activity类中的方法,只需指定mode
4. 返回Home的代码
Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN);intent.addCategory(Intent.CATEGORY_HOME); startActivity(intent);
5.ScrollView滚动到顶部
scrollView.post(new Runnable() { @Override public void run() { scrollView.scrollTo(0,0); } });不用线程还不行,NND!
6. 动画结束后执行某些动作。
Animation hang_fall = AnimationUtils.loadAnimation(Curriculum.this, R.anim.hang_fall ); hang_fall.setAnimationListener(new Animation.AnimationListener() { public void onAnimationEnd(Animation animation) { Intent i = new Intent( ThisActivity.this, NextActivity.class ); ThisActivity.this.startActivity( i ); } public void onAnimationRepeat(Animation animation) { // Do nothing! } public void onAnimationStart(Animation animation) { // Do nothing! } }); v.startAnimation( hang_fall );
7. 自定义SeekBar常见问题
1. 自定义的圆球高度小于进度条的高度
解决方法:
android:layout_height="wrap_content" //高度自适应,以便能容下圆球的高度
android:minHeight="10dip" android:maxHeight="10dip" //指定进度条的高度,让高度比球小就行(关键)
2. 自定义的圆球在进度条左边和右边被挡住
解决方法:
android:thumbOffset="0dp" / /调整这个值,默认情况下为8px (或者对圆球图片进行处理,左右各留一部分透明空间)
8. 很多朋友在用隐式Intent跳转Activity时, 遇到如下错误:
action对应肯定没问题,那问题出在哪呢? 是AndroidManifest配置文件中缺少了Caterogy的配置。
Android对待所有传递给Context.startActivity()的隐式intent 至少包含"android.intent.category.DEFAULT"(对应CATEGORY_DEFAULT常量)。因此,活动想要接收隐式intent必须要在intent过滤器中包含"android.intent.category.DEFAULT"。
注意:"android.intent.action.MAIN" 和 "android.intent.category.LAUNCHER"设置,它们分别标记活动开始新的任务和带到启动列表界面。它们可以包含"android.intent.category.DEFAULT"到种类列表,也可以不包含。
9. 自定义ListView、ExpandableListView快速滑动块 (代码中elv代表ExpandableListView实例)
try {
// 反射
Field f = AbsListView.class.getDeclaredField("mFastScroller");
f.setAccessible(true);
Object o= f.get(elv);
f = f.getType().getDeclaredField("mThumbDrawable");
f.setAccessible(true);
Drawable drawable = (Drawable) f.get(o);
drawable = getResources().getDrawable(R.drawable.icon);
f.set(o,drawable);
} catch (Exception e) {
Logger.e(TAG, e.getMessage(), e);
}
1070

被折叠的 条评论
为什么被折叠?



