2011-10-11 32 views
4

在我的應用程序中,我使用viewPager給我很好的swipey視圖。我想讓鍵盤隱藏在其中的兩頁中,但總是在一頁上顯示,我有一個文本框。不能讓Android軟鍵盤出現/隱藏使用viewpager

我試過各種方式讓鍵盤顯示,但它不工作。我想我必須在錯誤的地方調用顯示鍵盤代碼。

@Override 
public Object instantiateItem(View collection, int position) 
{ 
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = null; 
    if(position==0){ 
     layout=inflater.inflate(R.layout.other, null); 
     //new PC().create(layout, context); 
     ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(collection.getWindowToken(), 0); 
    }else if(position==1){ 
     layout=inflater.inflate(R.layout.main, null); 
     new BlurayRemote().create(layout,context); 
     ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(collection.getWindowToken(), 0); 
    }else if(position==2){ 
     layout=inflater.inflate(R.layout.text, null); 
     new TextInput().create(layout,context); 
     ((InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInputFromInputMethod(collection.getWindowToken(), 0); 
    } 

    ((ViewPager) collection).addView(layout); 

    return layout;  
} 

任何幫助將是偉大的,因爲它是讓我瘋了!

+0

此外,您的活動將在您旋轉屏幕時重新創建,因此上下文將會更改;如此一來,您的pageadaptor將保持不再存在的上下文,並導致內存泄漏! – Raunak

回答

1

我發現使用錯誤的上下文通常會把事情搞砸。嘗試這個。

@Override 
public Object instantiateItem(View collection, int position) 
{ 
    LayoutInflater inflator = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View layout = null; 
    if(position==0){ 
     layout=inflater.inflate(R.layout.other, null); 
     //new PC().create(layout, context); 
     ((InputMethodManager)collection.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(collection.getWindowToken(), 0); 
    }else if(position==1){ 
     layout=inflater.inflate(R.layout.main, null); 
     new BlurayRemote().create(layout, collection.getContext()); 
     ((InputMethodManager)collection.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(collection.getWindowToken(), 0); 
    }else if(position==2){ 
     layout=inflater.inflate(R.layout.text, null); 
     new TextInput().create(layout,collection.getContext()); 
     ((InputMethodManager)collection.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInputFromInputMethod(collection.getWindowToken(), 0); 
    } 

    ((ViewPager) collection).addView(layout); 

    return layout;  
} 
+0

這是第一次運作,但稍後不再有效。任何想法爲什麼? – smee204

2

我敢肯定有更好的方式來做到這一點,但我有同樣的問題,我周圍有由父母View設置爲可成爲焦點。這樣,無論是什麼導致軟鍵盤彈出都不會在您在頁面之間滑動時獲得焦點。

<!-- Dummy item to prevent your View from receiving focus --> 
<LinearLayout 
    ... 
    android:focusable="true" 
    android:focusableInTouchMode="true" /> 

    <!-- The view(s) that are causing the keyboard to pop up each time you swipe --> 
    <EditText ... /> 

</LinearLayout> 
+0

我真的很喜歡這個解決方法:D – Shehabix