不能得到android软键盘显示/隐藏使用viewpager



在我的应用程序中,我使用viewPager给我漂亮的滑动视图。我希望键盘隐藏在2页,但总是显示在一页,在那里我有一个文本框。

我已经尝试了各种方法来让键盘显示,但它只是不工作。我想我一定是在错误的地方调用了显示键盘代码。

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

任何帮助将是伟大的,因为它是驱使我发疯!

我确信有更好的方法来做到这一点,但我有同样的问题,我通过设置父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>

我发现使用错误的上下文通常会把事情搞砸。试试这个。

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

最新更新