cant get the android soft keyboard to appear/hide using viewpager
In my application I am using viewPager to give me nice swipey views. I want the keyboard to be hidden in 2 of the pages but always diaplayed on one page, where I have a text box.
I have tried various ways to get the keyboard to display but it just does not work. I think I must be calling the display keyboard code in the wrong place.
@Override
public Object instantiateItem( View collection, int position )
{
Layo开发者_开发技巧utInflater 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;
}
Any help would be great as it is driving me mad!
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>
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;
}
精彩评论