2013-10-22 94 views
1

我在scrollView中有一個按鈕。當按鈕被點擊時,只要點擊按鈕,我需要一個PopupWindow來顯示軟鍵盤。這是我的代碼看起來今天:Android:PopupWindow填充鍵盤上方的空間

final Button b = (Button) findViewById(R.id.button); 

     b.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); 
       View popupView = layoutInflater.inflate(R.layout.popup, null); 
       final PopupWindow popupWindow = new PopupWindow(popupView,WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.MATCH_PARENT,true); 
       popupWindow.showAtLocation(v.getRootView(), Gravity.FILL, 0, 0); 

       //Bring soft keyboard up : NOT WORKING 
       final InputMethodManager mInputMethodManager = (InputMethodManager) getBaseContext().getSystemService(Context.INPUT_METHOD_SERVICE); 
       EditText editText = (EditText) popupView.findViewById(R.id.editText); 
       mInputMethodManager.showSoftInput(editText, 0); 

      } 
     }); 

和我的佈局XML是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <EditText 
     android:id="@+id/yourViewsEditText" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:hint="Your Views" 
     > 
    </EditText> 



</RelativeLayout> 

當我嘗試FILL_PARENT代替WRAP_CONTENT,它通過填充整個屏幕開始了。我想實現這樣的事情:enter image description here

+0

它現在看起來怎麼樣? – waqaslam

回答

2

也許嘗試使用下面的XML佈局:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 

    android:isScrollContainer="true" > 


    <EditText 
     android:id="@+id/yourViewsEditText" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:hint="Your Views" /> 

</RelativeLayout> 

它應該自動設置佈局的高度,直到軟鍵盤啓動。

+1

真棒..謝謝Waqas。這是否帶來了鍵盤: popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); \t \t \t \t popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED); – sharath

1

你可以在鍵盤的HIGHT這樣的:

myLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { 

       @Override 
       public void onGlobalLayout() { 
        // TODO Auto-generated method stub 
        Rect r = new Rect(); 
        parent.getWindowVisibleDisplayFrame(r); 

        int screenHeight = parent.getRootView().getHeight(); 
        int heightDifference = screenHeight - (r.bottom - r.top); 
        loadDialog(heightDifference); 

       } 
      }); 

,然後你可以用鍵盤的高度和的寬度加載popupwindow設備:

public void loadDialog(int height) 
{ 
Display mDisplay = activity.getWindowManager().getDefaultDisplay(); 
int width = mDisplay.getWidth(); 

     //load the popup window with the height and width... 

}