-1

我想检查屏幕上是否可见软键盘。为此,我找到了一个代码并按照给定的方式尝试了它。但两者都 activityRootView.getRootView().getHeight()返回activityRootView.getHeight()零。我需要你的专家意见。

代码:

LayoutInflater inflator5=   
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View    row5 =     
inflator5.inflate(R.layout.order_confirmation_list2,null);



final LinearLayout activityRootView = (LinearLayout)row5. 
findViewById(R.id.orderconfirmRootId);


// calculate the difference between the root view height and the window view height
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();

// if more than 100 pixels, its probably a keyboard...
if (heightDiff > 100) {

// keyboard is visible, do something here

Toast toast = Toast.makeText(context, "keyboard visible!!!"   , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

} else {

// keyboard is not visible, do something here
       }

更新这个问题:

我遵循了许多与此类型相关的答案,但没有找到合适的答案。请浏览我新添加的代码。

当软键盘可见和隐藏时,如何在 getview() 方法中获取布局的高度差异?我在下面给出的代码中尝试了这个 ((EditText) view.findViewById(R.id.editTextQtyId)).addTextChangedListener。但总是通过 getHeight() 方法返回零。
随时欢迎您的专家观点。

新代码:

@Override
public View getView(int i, final View view, final ViewGroup viewgroup) {
    row=view;
    holder=null;
    try
    {
        if(row == null)
        {

            Button RemoveOrderBtn;
            LayoutInflater inflator=
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflator.inflate(R.layout.add_to_orderlist2,viewgroup , false);
            holder=new MyViewHolder(row);
            row.setTag(holder);
            Log.d("newrows", "new row");

        }
        else
        {

        holder= (MyViewHolder) row.getTag();
        Log.d("recycling", "Recycling stuff");

        }
    }
    catch(Exception e)
    {
        e.printStackTrace();

    }


try
{

SingleRow temp=list.get(i);
holder.Dno.setText(temp.Dno);
holder.Size.setText(temp.size);
holder.Mrp.setText(temp.Mrp);
holder.Color.setText(temp.color);
holder.SingleOrderTotal.setText(temp.val_total);
holder.P_Category.setText(temp.Pcategory);
holder.editTextQtyId.setText(temp.val_count);
File sdCard = Environment.getExternalStorageDirectory();
File file = new File(sdCard.getAbsolutePath() + "/App" +temp.imageName);
FileInputStream streamIn;
  try {
    streamIn = new FileInputStream(file);
    Bitmap bitmap = BitmapFactory.decodeStream(streamIn); 
    holder.Image.setImageBitmap(bitmap);
    streamIn.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
    }

catch(Exception e)
{
    e.printStackTrace();
}


 try
    {


     if(view!=null)
         {              
            ((EditText) view.findViewById(R.id.editTextQtyId)).setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View arg0, MotionEvent arg1) {

                    ((EditText) view.findViewById(R.id.editTextQtyId)).addTextChangedListener(new TextWatcher() {

                        @Override
                        public void afterTextChanged(Editable arg0) {

                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0, int arg1,
                                int arg2, int arg3) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onTextChanged(CharSequence arg0, int arg1,
                                int arg2, int arg3) {
                            // TODO Auto-generated method stub


                            LayoutInflater inflator5=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            View    row5 = inflator5.inflate(R.layout.order_confirmation_list2,null);
                            final LinearLayout layout = (LinearLayout)row5. findViewById(R.id.orderconfirmRootId);
                             Log.d("Log", "Height: " + layout.getHeight());
                             Toast toast = Toast.makeText(context, "Height= "+layout.getHeight() , Toast.LENGTH_SHORT);
                             toast.setGravity(Gravity.CENTER, 0, 0);
                             toast.show();

                        }

                    });

                    return false;
                }
            });
         }

    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
return row;
}
4

1 回答 1

1

你想在onCreate()方法中获得高度吗?

视图尚未在onCreate()onStart()或中构建onResume()。由于它们在技术上不存在(就目前ViewGroup而言),它们的尺寸为 0。

你应该使用OnGlobalLayoutListener.

像这样的东西:

//parent
ViewTreeObserver vto = v.getViewTreeObserver();  
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
            @SuppressWarnings("deprecation")
            @Override  
            public void onGlobalLayout() {  
                if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) 
                    v.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                else
                    v.getViewTreeObserver().removeOnGlobalLayoutListener(this);


                View myView = v.findViewById(R.id.my_view);
                // here height is not 0
                int h = myView.getHeight();

            }  
        }); 
于 2014-05-07T11:30:13.250 回答