键盘隐藏了scrollview内的edittext

时间:2014-09-04 08:46:37

标签: android scrollview android-linearlayout android-softkeyboard

<ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/btnAddRow"
        android:layout_below="@id/llInventoryViewHeader"
        android:isScrollContainer="true"
        android:scrollbars="vertical" >

        <LinearLayout
            android:id="@+id/llDynamicView"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

以线性布局设置行的代码: -

/**
     * Set Edit text
     */
    private void setUsedList() {

        for (final InventoryResource inventoryResource : mCurrentUsedResourceList) {
            final LinearLayout LL = new LinearLayout(InventoryActivity.this);
            LL.setOrientation(LinearLayout.HORIZONTAL);

            final LayoutParams LLParams = new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);

            LL.setWeightSum(10f);
            LL.setLayoutParams(LLParams);

            // ResourceName Params
            final LinearLayout.LayoutParams resourceViewParams = new LinearLayout.LayoutParams(
                    0, LinearLayout.LayoutParams.WRAP_CONTENT);
            resourceViewParams.weight = 6f;
            resourceViewParams.setMargins(5, 5, 5, 5);

            // Resource Edittext
            final EditText edtTextResourceName = new EditText(
                    InventoryActivity.this);
            edtTextResourceName.setGravity(Gravity.CENTER);
            edtTextResourceName.setLayoutParams(resourceViewParams);
            edtTextResourceName.setInputType(InputType.TYPE_CLASS_TEXT);
            edtTextResourceName.setTextColor(Color.BLACK);
            edtTextResourceName.setTextSize(16f);
            edtTextResourceName.setBackground(getResources().getDrawable(
                    R.drawable.box_edt_values));

            // Amount Params
            final LinearLayout.LayoutParams amtViewParams = new LinearLayout.LayoutParams(
                    0, LinearLayout.LayoutParams.WRAP_CONTENT);
            amtViewParams.weight = 2f;
            amtViewParams.setMargins(5, 5, 5, 5);

            final EditText edtTextConstructorAmt = new EditText(
                    InventoryActivity.this);
            edtTextConstructorAmt.setGravity(Gravity.CENTER);
            edtTextConstructorAmt.setInputType(InputType.TYPE_CLASS_PHONE);
            edtTextConstructorAmt.setLayoutParams(amtViewParams);
            edtTextConstructorAmt.setTextColor(Color.BLACK);
            edtTextConstructorAmt.setTextSize(16f);
            edtTextConstructorAmt.setBackground(getResources().getDrawable(
                    R.drawable.box_edt_values));


            final EditText edtTextInspectorAmt = new EditText(
                    InventoryActivity.this);
            edtTextInspectorAmt.setInputType(InputType.TYPE_CLASS_PHONE);
            edtTextInspectorAmt.setGravity(Gravity.CENTER);
            edtTextInspectorAmt.setLayoutParams(amtViewParams);
            edtTextInspectorAmt.setTextColor(Color.BLACK);
            edtTextInspectorAmt.setTextSize(16f);
            edtTextInspectorAmt.setBackground(getResources().getDrawable(
                    R.drawable.box_edt_values));

            final InventoryPojo pojo = new InventoryPojo();
            pojo.id = inventoryResource.getOrderNum();
            mOrderNumber += 1;
            pojo.edtResourceName = edtTextResourceName;
            pojo.edtConstructoreAmt = edtTextConstructorAmt;
            pojo.edtInspectoreAmt = edtTextInspectorAmt;
            mUsedList.add(pojo);

            if (mPreference.getString(Preferences.LAN_CULTURE,
                    Constants.CULTURE_HEBREW).equalsIgnoreCase(
                    Constants.CULTURE_ENGLISH)) {
                LL.addView(edtTextResourceName);
                LL.addView(edtTextConstructorAmt);
                LL.addView(edtTextInspectorAmt);
                mLLDetails.addView(LL);
                mLLDetails.invalidate();
            } else {
                LL.addView(edtTextInspectorAmt);
                LL.addView(edtTextConstructorAmt);
                LL.addView(edtTextResourceName);
                mLLDetails.addView(LL);
                mLLDetails.invalidate();
            }
        }
    }

代码: -

parent = (RelativeLayout)findViewById(R.id.rlParent);

parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
            public void onGlobalLayout(){
                  int heightDiff = parent.getRootView().getHeight()- parent.getHeight();
                  // IF height diff is more then 150, consider keyboard as visible.
                  if(heightDiff > 150){
                      // Its keyboard mostly
                      parent.setPadding(0, 0, 0, heightDiff);
                  }
                  else if(heightDiff < 150){
                       // Keyboard goes away, readjust
                      parent.setPadding(0, 0, 0, 0);
                  }
               }
         });

我在其中有滚动视图,其中添加了动态行。我面临的问题是,如果我的视图有10行,那么当我开始输入时,它不会滚动到最后。对于例如在10行的情况下,我能够向上滚动到7行,然后其他3行不可见,并且必须通过按回来关闭键盘,然后我可以将值添加到休息3行。

我在活动清单中添加了inputMode到adjustPan,并添加了android:isScrollContainer =&#34; true&#34;但它还是不起作用。

任何人都知道如何解决它。

3 个答案:

答案 0 :(得分:2)

好的,试试这可能会保存你的原因,

  1. 首先在您的主XML中保存此Scroll视图,为其根提供了一个特权,即其父

  2. 其次,android会为您提供一个API,告诉您在绘制之前分配给它们的视图的尺寸,您可以通过ViewTreeObservers阅读

  3. 使用此代码检查键盘何时充气,当其充气时,您可以将高度差异指定为填充底部到父视图,当键盘消失时只需重置填充集。这将确保您可以滚动隐藏在充气键盘下方的所有视图。

    parent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
        public void onGlobalLayout(){
            int heightDiff = parent.getRootView().getHeight()- parent.getHeight();
            // IF height diff is more then 150, consider keyboard as visible.
            if(heightDiff > 150){
                // Its keyboard mostly
                parent.setPadding(0, 0, 0, heightDiff);
            }
            else if(heightDiff < 150){
                // Keyboard goes away, readjust
                parent.setPadding(0, 0, 0, 0);
            }
       }
    });
    
  4. 4确保您在最活文件

    中的活动中定义了此参数
    android:windowSoftInputMode="adjustResize"
    

答案 1 :(得分:1)

您可能想要使用windowSoftInputMode。这是一个有趣的discussion,帮助我解决了类似的问题。

希望这能帮到你!

答案 2 :(得分:0)

你的滚动视图似乎很好。 要进行滚动,您只需在最明显的文件活动中添加单行。

机器人:windowSoftInputMode =&#34; adjustResize&#34;