无法使用ViewPager显示/隐藏Android软键盘

4
在我的应用程序中,我使用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;      
}

任何帮助都将是非常棒的,因为它让我发疯了!

此外,当您旋转屏幕时,您的活动将被重新创建,因此上下文将发生变化;因此,在某种程度上,您的页面适配器正在保持一个不再存在的上下文,并将导致内存泄漏! - Raunak
2个回答

3

我相信有更好的方法来解决这个问题,但是我遇到了同样的问题并通过将父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>

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

这个在第一次运行时可以工作,但过一会儿就不再工作了。有什么想法为什么会这样? - smee204

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接