Handling Soft Keyboard Navigation Arrows

瘦欲@ 提交于 2019-12-07 19:56:04

问题


Here i am using ViewPager for Fragments, i used custom view pager for controlling navigation. here is the class,

public class NonSwipeableViewPager extends ViewPager {

public NonSwipeableViewPager(Context context) {
    super(context);
    setMyScroller();
}

public NonSwipeableViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    setMyScroller();
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    // Never allow swiping to switch between pages
    return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    // Never allow swiping to switch between pages
    return false;
}

//down one is added for smooth scrolling

private void setMyScroller() {
    try {
        Class<?> viewpager = ViewPager.class;
        Field scroller = viewpager.getDeclaredField("mScroller");
        scroller.setAccessible(true);
        scroller.set(this, new MyScroller(getContext()));
    }catch (Exception e) {
        e.printStackTrace();
    }
}

public class MyScroller extends Scroller {
    public MyScroller(Context context) {
        super(context, new DecelerateInterpolator());
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        super.startScroll(startX, startY, dx, dy, 350 /*1 secs*/);
    }
}

}

There no Swipe and Click Events working. but what is my problem is

when i select FIRST NAME and click on the arrow button on my tablet (Samsung) it navigates to MIDDLE NAME then to LAST NAME, after LAST NAME it NAVIGATES TO THE NEXT FRAGMENT ie; INTRODUCTION tab on my view pager. this is my problem, how to prevent this?

what i tried is, i set EditText property nextFocusForward, and tried

 v.setFocusableInTouchMode(true); 
 v.requestFocus(); 
 v.setOnKeyListener(new View.OnKeyListener() { 
    @Override 
    public boolean onKey(View v, int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        if (keyCode == KeyEvent.KEYCODE_NAVIGATE_NEXT) {
            return false; 
          } 
      } 
      return false; 
    } 
}); 

but no good!
how to hide that soft key ? or how to control the action? the problem is only with this keyboard,(on Samsung Tab) on my phone there is no keys like this. how to get rid of this problem?


回答1:


I know its too late but may it help someone.

Try to use dispatchKeyEvent(KeyEvent event) in your Project

@Override public boolean dispatchKeyEvent(KeyEvent event) {

     Log.i("key pressed", String.valueOf(event.getKeyCode()));
     if(event.getKeyCode()==KeyEvent.KEY_A/*select required keycode*/){

        //perform required  task

     }
     return super.dispatchKeyEvent(event); 
}

it is returning you a keyCode check the keycode for navigation_next button.

Hope this will help you.



来源:https://stackoverflow.com/questions/43556148/handling-soft-keyboard-navigation-arrows

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!