3

我已经使用 ViewPager 实现了三个选项卡。在右侧选项卡布局中,我有一个编辑文本小部件,当用户单击它时,键盘会出现。如果用户不手动关闭键盘并滑动到中间或左侧选项卡,则键盘将保留在屏幕上。那么如何避免在我不需要的其他两个选项卡中使用键盘。

编辑:这是我的代码。

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


      } }

第二个文件: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;
}

TitlePageIndicator 看起来像这样:TitlePageIndicator

4

3 回答 3

2

在我看来,这不是一个严重的问题。用户打开键盘进行文本输入,并在不再需要时将其隐藏。

引用您的 EditText 对象,这应该隐藏键盘:

InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(editText.getWindowToken(), 0);

离开右侧选项卡时调用上面的代码段。

于 2013-01-30T09:10:31.483 回答
1

试试这个。

首先,监听 ViewPager.OnPageChangeListener 事件。然后检查软键盘是否可见,如果它显示在不需要的页面中,则使用以下代码将其隐藏:

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
if(imm != null){
    imm.toggleSoftInput(0, InputMethodManager.HIDE_IMPLICIT_ONLY);
}

希望有帮助。:)

于 2013-01-30T09:07:34.830 回答
1

如果它对某人有帮助,我想出了这个:

实用程序类

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

在 MyPagerAdapter 类里面

 @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);
    } 
}
于 2017-02-23T13:44:07.623 回答