android关于软键盘Utils

1.软键盘的使用

1.显示隐藏软键盘

获取系统服务类,让服务类来操作

private InputMethodManager utilInputMethodManager;
utilInputMethodManager = (InputMethodManager) MainActivity.this.getSystemService(Context.INPUT_METHOD_SERVICE);
1.显示隐藏:

隐藏

utilInputMethodManager.hideSoftInputFromWindow(etTest.getWindowToken(),0);

显示

utilInputMethodManager.showSoftInput(etTest,0);
2.区别:

共同点:都是两个参数,都是editText
不同点:第一个是EditText,另一个是EditText.getWindowToken();

2.软键盘问题

1.软键盘遮挡键盘问题

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//解决EditText被键盘遮挡问题

3.软键盘封装类

键盘显示隐藏监听,键盘显示隐藏

/**
 * 创建日期:2021/8/19 13:59
 *
 * @author tony.sun
 * 类说明:
 */

public class UtilKeyboardStateObserver {
    private static Activity activity;


    private static final String TAG = UtilKeyboardStateObserver.class.getSimpleName();

    public static UtilKeyboardStateObserver getKeyboardStateObserver(Activity activity) {
        return new UtilKeyboardStateObserver(activity);
    }
    private static InputMethodManager utilInputMethodManager;
    private View mChildOfContent;
    private int usableHeightPrevious;
    private OnKeyboardVisibilityListener listener;
    //显示键盘
    public static void show(Activity activity, EditText et) {
        utilInputMethodManager= (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        utilInputMethodManager.showSoftInput(et,0);
    }
    //隐藏键盘
    public static void hide(Activity activity, EditText et) {
        utilInputMethodManager= (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        utilInputMethodManager.hideSoftInputFromWindow(et.getWindowToken(),0);
    }
    //这个是软键盘显示隐藏的监听
    public void setKeyboardVisibilityListener(OnKeyboardVisibilityListener listener) {
        this.listener = listener;
    }

    private UtilKeyboardStateObserver(Activity activity) {
        FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
        mChildOfContent = content.getChildAt(0);
        mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                possiblyResizeChildOfContent();
            }
        });
    }

    private void possiblyResizeChildOfContent() {
        int usableHeightNow = computeUsableHeight();
        if (usableHeightNow != usableHeightPrevious) {
            int usableHeightSansKeyboard = mChildOfContent.getRootView().getHeight();
            int heightDifference = usableHeightSansKeyboard - usableHeightNow;
            if (heightDifference > (usableHeightSansKeyboard / 4)) {
                if (listener != null) {
                    listener.onKeyboardShow();
                }
            } else {
                if (listener != null) {
                    listener.onKeyboardHide();
                }
            }
            usableHeightPrevious = usableHeightNow;
            Log.d(TAG,"usableHeightNow: " + usableHeightNow + " | usableHeightSansKeyboard:" + usableHeightSansKeyboard + " | heightDifference:" + heightDifference);
        }
    }

    private int computeUsableHeight() {
        Rect r = new Rect();
        mChildOfContent.getWindowVisibleDisplayFrame(r);

        Log.d(TAG,"rec bottom>" + r.bottom + " | rec top>" + r.top);
        return (r.bottom - r.top);// 全屏模式下: return r.bottom
    }

    public interface OnKeyboardVisibilityListener {
        void onKeyboardShow();

        void onKeyboardHide();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android环形进度条是一种常见的UI组件,用于显示任务的进度。在Android开发中,可以使用Utils工具类来实现环形进度条的功能。 首先,你需要在项目中引入相关的依赖库。在build.gradle文件中添加以下代码: ``` implementation 'com.github.lzyzsd:circleprogress:1.2.1' ``` 接下来,你可以创建一个Utils工具类,用于封装环形进度条的相关方法。以下是一个简单的示例: ```java import android.content.Context; import android.graphics.Color; import android.view.ViewGroup; import android.widget.LinearLayout; import com.github.lzyzsd.circleprogress.DonutProgress; public class ProgressUtils { public static DonutProgress createCircularProgressBar(Context context, int progress, int max) { DonutProgress progressBar = new DonutProgress(context, null); progressBar.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); progressBar.setMax(max); progressBar.setProgress(progress); progressBar.setFinishedStrokeColor(Color.BLUE); progressBar.setUnfinishedStrokeColor(Color.GRAY); progressBar.setFinishedStrokeWidth(10); progressBar.setUnfinishedStrokeWidth(10); progressBar.setSuffixText("%"); progressBar.setTextSize(20); return progressBar; } } ``` 在上述代码中,我们使用了第三方库`com.github.lzyzsd:circleprogress`来创建环形进度条。通过`createCircularProgressBar`方法,我们可以传入进度和最大值来创建一个环形进度条,并设置相关的样式属性。 使用示例: ```java DonutProgress progressBar = ProgressUtils.createCircularProgressBar(context, 50, 100); ``` 这样就可以创建一个进度为50%的环形进度条,并将其添加到布局中显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值