首页实现页面切换
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package com.example.myapplication.util;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.graphics.Rect;
|
||||
import android.os.Build;
|
||||
import android.view.View;
|
||||
import android.view.ViewTreeObserver;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
/**
|
||||
* 2021 5 25
|
||||
*/
|
||||
|
||||
public class SoftHideKeyBoardUtil {
|
||||
public static void assistActivity(Activity activity) {
|
||||
new SoftHideKeyBoardUtil(activity);
|
||||
}
|
||||
|
||||
private View mChildOfContent;
|
||||
private int usableHeightPrevious;
|
||||
private FrameLayout.LayoutParams frameLayoutParams; //为适应华为小米等手机键盘上方出现黑条或不适配
|
||||
private int contentHeight;//获取setContentView本来view的高度
|
||||
private boolean isfirst = true;//只用获取一次
|
||||
private int statusBarHeight;//状态栏高度
|
||||
|
||||
private SoftHideKeyBoardUtil(Activity activity) { //1、找到Activity的最外层布局控件,它其实是一个DecorView,它所用的控件就是FrameLayout
|
||||
FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content); //2、获取到setContentView放进去的View
|
||||
mChildOfContent = content.getChildAt(0); //3、给Activity的xml布局设置View树监听,当布局有变化,如键盘弹出或收起时,都会回调此监听
|
||||
mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { //4、软键盘弹起会使GlobalLayout发生变化
|
||||
public void onGlobalLayout() {
|
||||
if (isfirst) {
|
||||
contentHeight = mChildOfContent.getHeight();//兼容华为等机型
|
||||
isfirst = false;
|
||||
} //5、当前布局发生变化时,对Activity的xml布局进行重绘
|
||||
possiblyResizeChildOfContent();
|
||||
}
|
||||
}); //6、获取到Activity的xml布局的放置参数
|
||||
frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
|
||||
} // 获取界面可用高度,如果软键盘弹起后,Activity的xml布局可用高度需要减去键盘高度
|
||||
|
||||
private void possiblyResizeChildOfContent() { //1、获取当前界面可用高度,键盘弹起后,当前界面可用布局会减少键盘的高度
|
||||
int usableHeightNow = computeUsableHeight(); //2、如果当前可用高度和原始值不一样
|
||||
if (usableHeightNow != usableHeightPrevious) { //3、获取Activity中xml中布局在当前界面显示的高度
|
||||
int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight(); //4、Activity中xml布局的高度-当前可用高度
|
||||
int heightDifference = usableHeightSansKeyboard - usableHeightNow; //5、高度差大于屏幕1/4时,说明键盘弹出
|
||||
if (heightDifference > (usableHeightSansKeyboard / 4)) { // 6、键盘弹出了,Activity的xml布局高度应当减去键盘高度
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference + statusBarHeight;
|
||||
} else {
|
||||
frameLayoutParams.height = usableHeightSansKeyboard - heightDifference;
|
||||
}
|
||||
} else {
|
||||
frameLayoutParams.height = contentHeight;
|
||||
} //7、 重绘Activity的xml布局
|
||||
mChildOfContent.requestLayout();
|
||||
usableHeightPrevious = usableHeightNow;
|
||||
}
|
||||
}
|
||||
|
||||
private int computeUsableHeight() {
|
||||
Rect r = new Rect();
|
||||
mChildOfContent.getWindowVisibleDisplayFrame(r); // 全屏模式下:直接返回r.bottom,r.top其实是状态栏的高度
|
||||
return (r.bottom - r.top);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.example.myapplication.util;
|
||||
|
||||
import android.graphics.Rect;
|
||||
import android.view.View;
|
||||
import android.view.ViewTreeObserver;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Created by yzb_android on
|
||||
* 作用:监听软键盘
|
||||
*/
|
||||
|
||||
public class SoftKeyboardStateHelper implements ViewTreeObserver.OnGlobalLayoutListener {
|
||||
|
||||
public interface SoftKeyboardStateListener {
|
||||
void onSoftKeyboardOpened(int keyboardHeightInPx);
|
||||
void onSoftKeyboardClosed();
|
||||
}
|
||||
|
||||
private final List<SoftKeyboardStateListener> listeners = new LinkedList<SoftKeyboardStateListener>();
|
||||
private final View activityRootView;
|
||||
private int lastSoftKeyboardHeightInPx;
|
||||
private boolean isSoftKeyboardOpened;
|
||||
|
||||
public SoftKeyboardStateHelper(View activityRootView) {
|
||||
this(activityRootView, false);
|
||||
}
|
||||
|
||||
public SoftKeyboardStateHelper(View activityRootView, boolean isSoftKeyboardOpened) {
|
||||
this.activityRootView = activityRootView;
|
||||
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
|
||||
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
final Rect r = new Rect();
|
||||
//r will be populated with the coordinates of your view that area still visible.
|
||||
activityRootView.getWindowVisibleDisplayFrame(r);
|
||||
|
||||
final int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
|
||||
if (!isSoftKeyboardOpened && heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
|
||||
isSoftKeyboardOpened = true;
|
||||
notifyOnSoftKeyboardOpened(heightDiff);
|
||||
} else if (isSoftKeyboardOpened && heightDiff < 100) {
|
||||
isSoftKeyboardOpened = false;
|
||||
notifyOnSoftKeyboardClosed();
|
||||
}
|
||||
}
|
||||
|
||||
public void setIsSoftKeyboardOpened(boolean isSoftKeyboardOpened) {
|
||||
this.isSoftKeyboardOpened = isSoftKeyboardOpened;
|
||||
}
|
||||
|
||||
public boolean isSoftKeyboardOpened() {
|
||||
return isSoftKeyboardOpened;
|
||||
}
|
||||
|
||||
/**
|
||||
* Default value is zero (0)
|
||||
* @return last saved keyboard height in px
|
||||
*/
|
||||
public int getLastSoftKeyboardHeightInPx() {
|
||||
return lastSoftKeyboardHeightInPx;
|
||||
}
|
||||
|
||||
public void addSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
|
||||
listeners.add(listener);
|
||||
}
|
||||
|
||||
public void removeSoftKeyboardStateListener(SoftKeyboardStateListener listener) {
|
||||
listeners.remove(listener);
|
||||
}
|
||||
|
||||
private void notifyOnSoftKeyboardOpened(int keyboardHeightInPx) {
|
||||
this.lastSoftKeyboardHeightInPx = keyboardHeightInPx;
|
||||
|
||||
for (SoftKeyboardStateListener listener : listeners) {
|
||||
if (listener != null) {
|
||||
listener.onSoftKeyboardOpened(keyboardHeightInPx);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void notifyOnSoftKeyboardClosed() {
|
||||
for (SoftKeyboardStateListener listener : listeners) {
|
||||
if (listener != null) {
|
||||
listener.onSoftKeyboardClosed();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.example.myapplication.util;
|
||||
|
||||
import org.json.JSONException;
|
||||
import org.json.JSONObject;
|
||||
|
||||
//去掉空的字符
|
||||
public class Whetherisempty {
|
||||
public static String getClfz(String s) throws JSONException {
|
||||
JSONObject jsonObject = new JSONObject(s);
|
||||
if (String.valueOf(jsonObject.get("result")).equals("[]") || String.valueOf(jsonObject.get("result")).equals("")) {
|
||||
jsonObject.put("result", null);
|
||||
return jsonObject.toString();
|
||||
} else {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getCl(String s) {
|
||||
String s2 = s.substring(s.length() - 3, s.length() - 1);
|
||||
if (s2.equals("[]") || s2.equals("")) {
|
||||
return s.substring(0, s.lastIndexOf(",")) + "}";
|
||||
} else {
|
||||
return s;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user