2014-05-07 52 views
-1

我想檢查軟鍵盤是否在屏幕上可見。爲此,我找到了一個代碼並按照給定的方式嘗試了它。但是activityRootView.getRootView().getHeight()activityRootView.getHeight()都返回零。 我需要你的專家觀點。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 
     } 

更新到這個問題:

我跟着與此相關的許多類型的答案,但但沒有發現suitable.plz通過我新添加的代碼。

當軟鍵盤可見和隱藏時,如何獲得getview()方法內佈局高度的差異?我試過這裏面((EditText)view.findViewById(R.id.editTextQtyId))。addTextChangedListener在我的代碼給出如下。但總是通過getHeight()方法返回零。
您的專家視圖總是歡迎。

NEW CODE:

@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; 
} 
+0

請遵循本http://stackoverflow.com/a/19271643/942224 –

+0

的可能重複[Android開寬度,則返回0](http://stackoverflow.com/問題/ 3591784/android-get-width-returns-0) – Selvin

+0

plz檢查我的更新問題。 – user177913

回答

0

你想獲得高度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(); 

      } 
     }); 
+0

PLZ通過我新添加的代碼... – user177913