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

任何帮助都会很棒,因为它让我发疯!

4

2 回答 2

3

I'm sure there is a better way to do this, but I was having the same problem and I got around it by setting the parent View to focusable. That way, whatever is causing the soft keyboard from popping up will not receive focus when you swipe between pages.

<!-- 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>
于 2012-02-17T16:58:43.343 回答
1

I have found that using the wrong context screws up things usually. Try this.

@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;      
}
于 2011-10-13T15:51:38.660 回答