2011-10-11 77 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