关闭

软键盘的应用

23人阅读 评论(0) 收藏 举报

在安卓开发中软键盘是在评论模块中不可缺少的内容:


1.评论嵌套在scrollView中,这是评论的弹出是遮盖到评论的内容的,这是要用到scrollTO的方法;


其中parentLayout是根布局的main

parentLayout.getViewTreeObserver().addOnGlobalLayoutListener(
        new ViewTreeObserver.OnGlobalLayoutListener() {

            @Override
            public void onGlobalLayout() {
                Rect r = new Rect();
                // r will be populated with the coordinates of your view
                // that area still visible.
                parentLayout.getWindowVisibleDisplayFrame(r);
                DisplayMetrics metric = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(
                        metric);
                int screenHeight = metric.widthPixels; // 屏幕宽度(像素)
                int heightDiff = screenHeight - (r.bottom - r.top);
                if (heightDiff > 100)
                    // if more than 100 pixels, its probably a keyboard
                    // get status bar height

                    try {
                        Class<?> c = Class
                                .forName("com.android.internal.R$dimen");
                        Object obj = c.newInstance();
                        Field field = c.getField("status_bar_height");
                        int x = Integer.parseInt(field.get(obj)
                                .toString());
                        statusBarHeight = context.getResources()
                                .getDimensionPixelSize(x);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                realKeyboardHeight = heightDiff - statusBarHeight;

                if (realKeyboardHeight > 0) {
                    //这里的post是必不可少的。
                    scrollView.post(new Runnable() {
                        @Override
                        public void run() {
                            scrollView.scrollBy(0, 5 * realKeyboardHeight);
                        }
                    });
                }else {
                    scrollView.post(new Runnable() {
                        @Override
                        public void run() {
                            scrollView.scrollBy(0, -5 * realKeyboardHeight);
                        }
                    });
                }
            }
        });

rl_more.setFocusable(true);
rl_more.setFocusableInTouchMode(true);
rl_more.requestFocus();


还有一种就是监听软键盘的确认按钮:


tv.setOnEditorActionListener(this);

  public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        boolean isOK = true;
        switch (actionId) {
            case EditorInfo.IME_ACTION_NONE:
                Toast.makeText(this, "点击-->NONE", Toast.LENGTH_SHORT).show();
                break;
            case EditorInfo.IME_ACTION_GO:
                Toast.makeText(this, "点击-->GO", Toast.LENGTH_SHORT).show();
                break;
            case EditorInfo.IME_ACTION_SEARCH:
                Toast.makeText(this, "点击-->SEARCH", Toast.LENGTH_SHORT).show();
                break;
            case EditorInfo.IME_ACTION_SEND:
                Toast.makeText(this, "点击-->SEND", Toast.LENGTH_SHORT).show();
                break;
            case EditorInfo.IME_ACTION_NEXT:
                Toast.makeText(this, "点击-->NEXT", Toast.LENGTH_SHORT).show();
                break;
            default:
                isOK = false;
                break;
        }
        
       return isOK;

    }

以上两种是必备的。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:106次
    • 积分:41
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章存档