Есть ViewPager, на двух соседних страницах на одной высоте расположены два EditText - ввод номера телефона и проверочный код. Задача - при заходе на первую страницу встать на поле ввода номера и открыть клавиатуру. После ввода данных и нажатия Done, не закрывая клавиатуру, перейти на следующую страницу и встать на второе поле ввода.
Реализовано так:
<android.support.percent.PercentFrameLayout
android:id="@+id/verification"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/superup_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:layout_marginTopPercent="33%"
app:layout_widthPercent="65%"
app:srcCompat="@drawable/animated_superup_logo_white"/>
<superup.onboarding.NonSwipeableViewPager
android:id="@+id/verifyPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/transition">
<android.support.percent.PercentRelativeLayout
android:id="@+id/layout_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.AppCompatImageView
android:id="@+id/superup_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
app:layout_marginTopPercent="67%"
app:layout_widthPercent="50%"
app:srcCompat="@drawable/superup_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/superup_name"
android:layout_centerHorizontal="true"
android:text="@string/layout_start_text"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold"/>
</android.support.percent.PercentRelativeLayout>
<android.support.percent.PercentRelativeLayout
android:id="@+id/layout_number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr">
<TextView
android:id="@+id/txt_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="@string/layout_number_text"
android:textColor="@color/white"
android:textSize="18dp"
android:textStyle="bold"
app:layout_marginTopPercent="3%"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center"
app:layout_marginBottomPercent="23%"
app:layout_widthPercent="75%">
<superup.onboarding.CountryCodePicker
android:id="@+id/country_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:contentColor="@color/white"
app:countryPreference="IL"
app:defaultNameCode="IL"
app:hideNameCode="true"
app:textSize="24sp"/>
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="1234567890"
android:gravity="center"
android:imeOptions="actionDone"
android:inputType="number"
android:maxLength="10"
android:textColor="@color/white"
android:textSize="24sp"/>
</LinearLayout>
</android.support.percent.PercentRelativeLayout>
<android.support.percent.PercentRelativeLayout
android:id="@+id/layout_otp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<superup.onboarding.PinEntryEditText
android:id="@+id/et_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:cursorVisible="false"
android:digits="1234567890"
android:imeOptions="actionDone"
android:inputType="number"
android:maxLength="4"
android:textIsSelectable="false"
android:textSize="50dp"
android:textStyle="bold"
app:layout_marginBottomPercent="23%"
app:layout_marginLeftPercent="20%"
app:layout_marginRightPercent="20%"/>
<TextView
android:id="@+id/txt_wrong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/et_code"
android:layout_centerHorizontal="true"
android:padding="10dp"
android:text="@string/wrong_number"
android:textColor="@color/white"
android:textSize="16dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/txt_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/txt_wrong"
android:layout_centerHorizontal="true"
android:layoutDirection="ltr"
android:textColor="@color/white"
android:textSize="20dp"
android:textStyle="bold"/>
</android.support.percent.PercentRelativeLayout>
</superup.onboarding.NonSwipeableViewPager>
</android.support.percent.PercentFrameLayout>
verifyPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
int lastPosition = 0;
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//rotate logo on scroll between page 0-1
if (position < 1) {
superup_logo.setTranslationY(0);
superup_logo.setRotation(positionOffset * 90);
//remove delayed scroll on user swipe
if (positionOffset > 0)
removeCallbacks(handler, runnable);
}
//move logo up on scroll between page 1-2
else if (position < 2) {
superup_logo.setRotation(90f);
superup_logo.setTranslationY(-getResources().getDisplayMetrics().densityDpi / 2 * positionOffset);
}
//move logo down on scroll between page 2-3
else if (position < 3) {
float begin = superup_logo.getTop() - getResources().getDisplayMetrics().densityDpi / 2;
float end = (getResources().getDisplayMetrics().heightPixels - superup_logo.getMeasuredHeight()) / 2;
superup_logo.setY(begin + (end - begin) * positionOffset);
}
}
@Override
public void onPageSelected(int position) {
switch (position) {
case 0:
hideKeyboard();
setFirstPage(delay);
break;
case 1:
keyHandler = new Handler();
keyRunnable = new Runnable() {
@Override
public void run() {
et_number.setSelection(et_number.getText().length()); // set cursor at the end
showKeyboard(et_number); //open keyboard
}
};
keyHandler.postDelayed(keyRunnable, 1500); // 1500 ms delay
verifyPager.setPagingEnabled(false); // disable page swipe
break;
case 2:
et_code.setSelection(et_code.getText().length());
showKeyboard(et_code);
break;
case 3:
hideKeyboard();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//start the logo blink animation
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Drawable animation = superup_logo.getDrawable();
if (animation instanceof Animatable) {
((Animatable) animation).start();
}
}
}, 300);
}
//start logo scale down and the background animation
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
TransitionDrawable transition = (TransitionDrawable) verifyPager.getBackground();
Animation animationScaleDown = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_down);
superup_logo.startAnimation(animationScaleDown);
transition.startTransition(500);
animationScaleDown.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
// open another activity
SuperupApplication.getPlatformsManager().setLoginData(
ObservableContactsBook.formatPhoneNumber(getApplicationContext(), String.valueOf(txt_number.getText())));
startActivity(new Intent(LoginActivity.this, PlatformsActivity.class));
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}, 1000);
break;
}
lastPosition = position;
}
/**
* Remove 1500 ms delay for keyboard appearance (from page 1)
* and hide keyboard
*
* @param state int PageScrollState
*/
@Override
public void onPageScrollStateChanged(int state) {
// remove show keyboard delay handler
if (state == verifyPager.SCROLL_STATE_SETTLING) {
removeCallbacks(keyHandler, keyRunnable);
}
}
});
При переходе на вторую страницу по нажатию Done второй EditText оказывается под клавиатурой.
et_number.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
switch (actionId) {
case EditorInfo.IME_ACTION_DONE:
verifyPager.setCurrentItem(verifyPager.getChildCount() - 1);
return true;
default:
return false;
}
}
});
Все это проявляется на Самсунге с Android 6. На Nexus, китайцах все ок. День потратил на поиски решения, пока безуспешно. Может можно как-то иначе программно сделать свайп?