原文链接:http://blog.csdn.net/mr_liu_gege/article/details/53169359
拾人牙慧,感谢博主分享。试验了一下,确实有效果,收藏学习。

第一种情况:被键盘遮住的按钮没有位于布局的底部

代码:

            
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width= "match_parent"
  4. android:layout_height= "wrap_content"
  5. android:orientation= "vertical"
  6. android:id= "@+id/parent_ll">
  7. <EditText
  8. android:id= "@+id/username"
  9. android:layout_width= "match_parent"
  10. android:layout_height= "wrap_content"
  11. android:layout_marginTop= "200dp"
  12. android:ems= "10" >
  13. EditText>
  14. <EditText
  15. android:id= "@+id/userpwd"
  16. android:layout_width= "match_parent"
  17. android:layout_height= "wrap_content"
  18. android:layout_marginTop= "30dp"
  19. android:ems= "10"
  20. android:inputType= "textPassword" />
  21. <Button
  22. android:id= "@+id/btn"
  23. android:layout_width= "wrap_content"
  24. android:layout_height= "wrap_content"
  25. android:layout_gravity= "center"
  26. android:layout_marginTop= "30dp"
  27. android:text= "Button" />
  28. LinearLayout>
            
  1. public class MainActivity extends Activity {
  2. private Button btn;
  3. private LinearLayout parent_ll;
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.layout_login);
  8. /*ScrollView mScrollView = (ScrollView)findViewById(R.id.scrollContent);
  9. mScrollView.setVerticalScrollBarEnabled(false);
  10. mScrollView.setHorizontalScrollBarEnabled(false);*/
  11. parent_ll=(LinearLayout) findViewById(R.id.parent_ll);
  12. btn=(Button) findViewById(R.id.btn);
  13. addLayoutListener(parent_ll, btn);
  14. }
  15. /**
  16. * 1、获取parentView在窗体的可视区域
  17. * 2、获取parentView在窗体的不可视区域高度
  18. * 3、判断不可视区域高度
  19. * 1、大于100:键盘显示 获取childView的窗体坐标
  20. * 算出parentView需要滚动的高度,使childView显示。
  21. * 2、小于100:键盘隐藏
  22. *
  23. * @param parentView 根布局
  24. * @param childView 需要显示的最下方View
  25. */
  26. public void addLayoutListener(final View parentView, final View childView) {
  27. parentView.getViewTreeObserver().addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() {
  28. @Override
  29. public void onGlobalLayout() {
  30. Rect rect = new Rect();
  31. parentView.getWindowVisibleDisplayFrame(rect);
  32. int mainInvisibleHeight = parentView.getRootView().getHeight() - rect.bottom;
  33. if (mainInvisibleHeight > 100) {
  34. int[] location = new int[ 2];
  35. childView.getLocationInWindow(location);
  36. int srollHeight = (location[ 1] + childView.getHeight()) - rect.bottom;
  37. parentView.scrollTo( 0, srollHeight);
  38. } else {
  39. parentView.scrollTo( 0, 0);
  40. }
  41. }
  42. });
  43. }
  44. }
addLayoutListener() 这个方法是关键。

第二种情况:被键盘遮住的按钮位于布局的底部

父布局需要是Relativelayout,按钮需要alignParentBottom,可能清单文件还需要设置android:windowSoftInputMode

更多相关文章

  1. 《Android(安卓)UI基础教程》之读书笔记
  2. [android]如何使LinearLayout布局从右向左水平排列,而不是从左向
  3. 【android】小知识点整理qwq(三)
  4. android夜间模式的实现
  5. Android键盘的显示与隐藏
  6. Android(安卓)Material Design(一)史上最全的材料设计控件大全
  7. 解决webview中输入框完成输入隐藏键盘后滑动界面又弹出软键盘问
  8. Android(安卓)M新控件知识整理
  9. Android(安卓)多点触控屏蔽

随机推荐

  1. 如果没有 Android,世界会怎样?
  2. Android学习笔记1---简单计算器
  3. 在ubuntu上安装google play和安卓应用
  4. Android(安卓)项目导入eclipse中报错但找
  5. Android拍照实现方式概述
  6. ANDROID中按键的添加配置(linux按键到andr
  7. 利用HTML5开发Android笔记
  8. Android中原始资源文件使用详解
  9. Android设备之间通过Wifi通信的示例代码
  10. Android小项目之--找到本地联络人并向其