问题
I have implemented three tabs using ViewPager. In the right tab layout i have one edit text widget, when users click on it keyboard comes up. If the user don't close the keyboard manually and swipe to the middle or left tab the keyboard remains on screen. So how to avoid the keyboard in other two tabs where i don't need it.
Edit: Here's my code.
class ViewPagerActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
TitlePageIndicator titleIndicator = (TitlePageIndicator) findViewById(R.id.titles);
titleIndicator.setViewPager(myPager);
} }
Second File: MyPagerAdapter.java
class MyPagerAdapter extends PagerAdapter implements OnClickListener,
OnLongClickListener, AdapterView.OnItemSelectedListener {
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
View view;
switch (position) {
case 0:
resId = R.layout.left;
view = inflater.inflate(resId, null);
break;
case 1:
resId = R.layout.main_c;
view = inflater.inflate(resId, null);
btn_no1 = (Button) view.findViewById(R.id.one);
btn_no1.setOnClickListener(this);
((ViewPager) collection).addView(view, 0);
return view;
case 2:
resId = R.layout.right;
view = inflater.inflate(resId, null);
main_spinner = (Spinner) view.findViewById(R.id.spinner1);
main_spinner.setOnItemSelectedListener(this);
from_spinner = (Spinner) view.findViewById(R.id.spinner2);
from_spinner.setOnItemSelectedListener(this);
to_spinner = (Spinner) view.findViewById(R.id.spinner3);
to_spinner.setOnItemSelectedListener(this);
swap = (ImageButton) view.findViewById(R.id.swap_spinner);
swap.setOnClickListener(this);
((ViewPager) collection).addView(view, 0);
return view;
}
view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
And TitlePageIndicator looks like this: TitlePageIndicator
回答1:
In my opinion this is not a serious problem. The user opens the keyboard for text input and will hide it when it's no longer needed.
Having a reference to your EditText object, this should hide the keyboard:
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);
Call the snippet above when leaving the right tab.
回答2:
Try this.
First, listen to ViewPager.OnPageChangeListener event. Then check if soft keyboard is visible, if it is shown in the page where it is not needed, hide it by using this code:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}
Hope that helps. :)
回答3:
If it helps someone, I came out with this:
Utils class
public void hideKeyboard(Activity activity) {
if (activity == null || activity.getCurrentFocus() == null || activity.getCurrentFocus().getWindowToken() == null) return;
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
public void hideKeyboard(Fragment fragment) {
if (fragment == null || fragment.getActivity() == null || fragment.getActivity().getCurrentFocus() == null || fragment.getActivity().getCurrentFocus().getWindowToken() == null) return;
InputMethodManager inputManager = (InputMethodManager) fragment.getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(fragment.getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
and inside MyPagerAdapter class
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
//assuming fragment in position 2 should hide the keyboard
if (position == 2) {
new SystemGlobal().hideKeyboard((Fragment) object);
}
}
来源:https://stackoverflow.com/questions/14600104/closing-keyboard-in-viewpager