1、EventBus注册问题 注册以及解绑之前判断是否注册过
注册:
if (!EventBus.getDefault().isRegistered(activity)) {
EventBus.getDefault().register(activity);
}
解绑:
if (EventBus.getDefault().isRegistered(activity)) {
EventBus.getDefault().unregister(activity);
}
2、RecyclerView item 包含 RadioButton 报错
解决办法:
radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (recyclerView.isComputingLayout()) {
recyclerView.post(runnable = new Runnable() {
@Override
public void run() {
positon = baseViewHolder.getAdapterPosition();
notifyItemChanged(baseViewHolder.getAdapterPosition());
}
});
} else {
positon = baseViewHolder.getAdapterPosition();
notifyDataSetChanged();
}
}
});
if (positon == baseViewHolder.getLayoutPosition()) {
radioButton.setChecked(true);
} else {
radioButton.setChecked(false);
}
if (runnable != null) {
recyclerView.removeCallbacks(runnable);
}
3、点击返回键 仿 home 键
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
4、Gilde使用要判断activity是否销毁
public static boolean isDestroy(Activity mActivity) {
if (mActivity == null || mActivity.isFinishing() || (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mActivity.isDestroyed())) {
return true;
} else {
return false;
}
}
使用:
public static void GlideImg(Context context, Object path, ImageView imageView) {
if (!isDestroy((Activity) context)) {
Glide.with(context).load(path).into(imageView);
}
}
5、WebView显示图文适配问题
private String metaType = "<head><meta name='viewport' content='width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0,user-scalable=no'> <style>img{max-width: 100%; width:auto; height:auto;}</style></head>";
使用:
mAgentWeb.getUrlLoader().loadDataWithBaseURL(null,metaType + noticeContent, "text/html", "UTF-8",null);
ps: noticeContent服务器返回的数据
6、EditText 密码显示与隐藏
隐藏:pwdEditText.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
显示:pwdEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
将光标移动到最后: pwdEditText.setSelection(pwdEditText.getText().length());
7、代码方式显示输入框长度
InputFilter[] inputFilter = {new InputFilter.LengthFilter(11)};
mEditText.setFilters(inputFilter);
8、解析一个 key 不是固定长度的json数组
private List<JsonLoginCityHeadInfo> loginHeadCityInfos = new ArrayList<>();
接口成功数据:
try {
if (loginHeadCityInfos != null && loginHeadCityInfos.size() > 0) {
loginHeadCityInfos.clear();
}
JSONObject jsonObject = new JSONObject(json);
String data = jsonObject.getString("data");
JSONObject jsonData = new JSONObject(data);
for (Iterator<String> iter = jsonData.keys(); iter.hasNext(); ) {
JsonLoginCityHeadInfo headInfo = new JsonLoginCityHeadInfo();
String key = iter.next();
JSONArray jsonArray = new JSONArray(jsonData.get(key).toString());
JSONObject jobAlias = jsonArray.getJSONObject(0);
List<JsonLoginCityInfo> list = new ArrayList<>();
String alias = jobAlias.getString("alias");
if ("主库".equals(alias)) {
continue;
}
for (int i = 0; i < jsonArray.length(); i++) {
JsonLoginCityInfo loginCityInfo = new JsonLoginCityInfo();
JSONObject job = jsonArray.getJSONObject(i);
loginCityInfo.setInclude(job.getString("字段"));
loginCityInfo.setCity_tag(job.getString("字段"));
list.add(loginCityInfo);
headInfo.setHeadList(list);
}
headInfo.setAlias(alias);
loginHeadCityInfos.add(headInfo);
}
} catch (JSONException e) {
e.printStackTrace();
}
9、killAppProcess
public void killAppProcess() {
try {
ActivityManager mActivityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> mList = mActivityManager.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo runningAppProcessInfo : mList) {
if (runningAppProcessInfo.pid != android.os.Process.myPid()) {
android.os.Process.killProcess(runningAppProcessInfo.pid);
}
}
android.os.Process.killProcess(android.os.Process.myPid());
System.exit(0);
} catch (Exception e) {
}
}
10、8.0前台服务 以及通知栏
前台服务开启方式:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this, YourService.class));
} else {
startService(new Intent(this, YourService.class));
}
创建通知栏
//通知栏相关参数
private static final String NOTIFICATION_CHANNEL_NAME = "YourAppName";
private final int KEEP_LIVE = 1001;
private NotificationManager notificationManager = null;
boolean isCreateChannel = false;
@SuppressLint("NewApi")
protected Notification buildNotification() {
Notification.Builder builder = null;
Notification notification = null;
if (android.os.Build.VERSION.SDK_INT >= 26) {
if (null == notificationManager) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
}
String channelId = getPackageName();
if (!isCreateChannel) {
NotificationChannel notificationChannel = new NotificationChannel(channelId,
NOTIFICATION_CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.BLUE);
notificationChannel.setShowBadge(true);
notificationManager.createNotificationChannel(notificationChannel);
isCreateChannel = true;
}
builder = new Notification.Builder(this, channelId);
} else {
builder = new Notification.Builder(this);
}
builder.setContentTitle(NOTIFICATION_CHANNEL_NAME )
.setContentText(NOTIFICATION_CHANNEL_NAME+"正在运行")
.setSmallIcon(R.mipmap.icon)
.setAutoCancel(false)
.setWhen(System.currentTimeMillis());
//开启一个广播 根据项目需要 非必须
// Intent intent = new Intent(this, IntentReceiver.class);
// intent.setAction("builder");
// PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// builder.setContentIntent(pendingIntent);
if (android.os.Build.VERSION.SDK_INT >= 16) {
notification = builder.build();
} else {
return builder.getNotification();
}
return notification;
}
11、Android 9.0 WebView 多进程报错问题
获取进程名字
public String getProcessName(Context context) {
try{
if (context == null) {
return null;
}
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningAppProcessInfo processInfo : manager.getRunningAppProcesses()) {
if (processInfo.pid == android.os.Process.myPid()) {
return processInfo.processName;
}
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
设置 setDataDirectorySuffix
public void setWebDataSuffixPath(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
String processName = getProcessName(context);
if (!"包名".equals(processName)) {
WebView.setDataDirectorySuffix(processName);
}
}
}
ps: 在Application 的 attachBaseContext 里面调用就可以
12、ButterKnife在Fragment里使用
private Unbinder unbinder;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(getLayout(), container, false);
unbinder = ButterKnife.bind(this, view);
return view;
}
@Override
public void onDestroy() {
super.onDestroy();
if (unbinder != null) {
unbinder.unbind();
}
}
13、点击外部取消键盘
//键盘是否弹出
private boolean isShouldHideKeyboard(View v, MotionEvent event) {
if ((v instanceof EditText)) {
int[] l = {0, 0};
v.getLocationOnScreen(l);
int left = l[0],
top = l[1],
bottom = top + v.getHeight(),
right = left + v.getWidth();
return !(event.getRawX() > left && event.getRawX() < right
&& event.getRawY() > top && event.getRawY() < bottom);
}
return false;
}
//动态隐藏键盘
private void hideSoftInput(Activity activity) {
View view = activity.getWindow().peekDecorView();
if (view != null) {
InputMethodManager inputmanger = (InputMethodManager) activity
.getSystemService(Context.INPUT_METHOD_SERVICE);
inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
//触摸外部键盘收回
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (ev.getAction() == MotionEvent.ACTION_DOWN) {
View v = getCurrentFocus();
if (isShouldHideKeyboard(v, ev)) {
hideSoftInput(this);
}
}
return super.dispatchTouchEvent(ev);
}
14、高德地图 显示所有的marker点
protected void zoomToSpanWithCenter(AMap aMap, List<LatLng> pointList, LatLng centerPoint) {
if (pointList != null && pointList.size() > 0) {
if (aMap == null) {
return;
}
LatLngBounds bounds = getLatLngBounds(centerPoint, pointList);
aMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));
}
}
private LatLngBounds getLatLngBounds(LatLng centerpoint, List<LatLng> pointList) {
LatLngBounds.Builder b = LatLngBounds.builder();
if (centerpoint != null) {
for (int i = 0; i < pointList.size(); i++) {
LatLng p = pointList.get(i);
LatLng p1 = new LatLng((centerpoint.latitude * 2) - p.latitude, (centerpoint.longitude * 2) - p.longitude);
b.include(p);
b.include(p1);
}
}
return b.build();
}
15、判断App处于后台还是前台
import android.app.Activity;
import android.app.Application;
import android.os.Bundle;
public class AppStateTracker {
public static final int STATE_FOREGROUND = 0;
public static final int STATE_BACKGROUND = 1;
private static int currentState;
public static int getCurrentState() {
return currentState;
}
public interface AppStateChangeListener {
void appTurnIntoForeground();
void appTurnIntoBackGround();
}
public static void track(Application application, final AppStateChangeListener appStateChangeListener){
application.registerActivityLifecycleCallbacks(new SimpleActivityLifecycleCallbacks(){
private int resumeActivityCount = 0;
@Override
public void onActivityStarted(Activity activity) {
if (resumeActivityCount==0){
currentState = STATE_FOREGROUND;
appStateChangeListener.appTurnIntoForeground();
}
resumeActivityCount++;
}
@Override
public void onActivityStopped(Activity activity) {
resumeActivityCount--;
if (resumeActivityCount==0){
currentState = STATE_BACKGROUND;
appStateChangeListener.appTurnIntoBackGround();
}
}
});
}
private static class SimpleActivityLifecycleCallbacks implements Application
.ActivityLifecycleCallbacks{
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
}
}
使用:
在Application 的 onCreate
AppStateTracker.track(this, new AppStateTracker.AppStateChangeListener() {
@Override
public void appTurnIntoForeground() {
//前台
}
@Override
public void appTurnIntoBackGround() {
//后台
}
});
16、动态修改TextView图片
Drawable drawable = ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_heating);
main_hot.setCompoundDrawablesWithIntrinsicBounds(null, drawable, null, null);
17、
在Module:app 下的 build.gradle 下的 defaultConfig 下 添加: resConfigs "zh"
18、
Gson解析失败 检查时候没有配置混淆。
19、TextView增加行间距
android:lineSpacingExtra 设置行间距,如”10dp”。
android:lineSpacingMultiplier 设置行间距的倍数,如”1.5或者2″。
20、使用HtmlCompat改变文字大小
HtmlCompat.fromHtml("Html 标签+内容", HtmlCompat.FROM_HTML_MODE_LEGACY)
21、BaseQuickAdapter item上的view点击事件
init {
addChildClickViewIds(view)
}
22、关于高德导航界面设置属性AMapNaviViewOptions不生效问题,界面底部设置无法去掉问题
检查APP主题设置
23、判断当前view是否在屏幕可见,配合ScrollView使用
fun isVIewVisible(context: Context, view: View, offsetY: Float): Boolean {
val p = Point()
(context as Activity).getWindowManager().getDefaultDisplay().getSize(p)
val screenWidth: Int = p.x
val screenHeight: Int = p.y
val rect = Rect(0, 0, screenWidth, screenHeight)
val location = IntArray(2)
location[1] = location[1] + RxTool.dip2px(offsetY)
view.getLocationInWindow(location)
view.tag = location[1] //存储y方向的位置
return if (view.getLocalVisibleRect(rect)) {
true
} else {
false
}
}
----------------------分割线----------------------
scrollView.setOnScrollChangeListener(object : NestedScrollView.OnScrollChangeListener {
override fun onScrollChange(v: NestedScrollView?, scrollX: Int, scrollY: Int, oldScrollX: Int, oldScrollY: Int) {
if (!getLocalVisibleRect(context, view, scrollY.toFloat())) {
//view不可见时操作
}
}
})
24、windowBackground设置启动图拉伸严重
drawable 里面新建splash.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:src="@mipmap/start_layout"
android:gravity="fill"/>
</item>
</layer-list>
设置主题
<style name="StartTheme" parent="AppTheme">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:navigationBarColor">@color/transparent</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowBackground">@drawable/splash</item>
</style>