您的位置:首页 > 其它

软键盘的应用

2017-01-03 12:31 363 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/zwhtiger/article/details/53993310

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


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;

    }

以上两种是必备的。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: