安卓屏幕我向下滚动每个输入到editText插入

时间:2014-01-29 21:35:36

标签: java android android-layout layout

我有以下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue_bg" >

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_horizontal"
                android:orientation="vertical" >


....
                <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.3" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:src="@drawable/signup_illu_verificationcode" />

            <View
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_weight="0.0" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:background="@android:color/white"
                android:padding="5dp"
                android:paddingLeft="25dp"
                android:paddingRight="25dp" >

                <LinearLayout
                    android:id="@+id/inputBox"
                    android:layout_width="match_parent"
                    android:layout_height="50dp"
                    android:layout_gravity="center"
                    android:layout_marginLeft="15dp"
                    android:layout_marginRight="15dp"
                    android:background="@drawable/input_box_idle"
                    android:gravity="center"
                    android:orientation="horizontal" >

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content" >

                        <EditText
                            android:id="@+id/verificationCodeEditText"
                            style="@style/textOnBg"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@android:color/transparent"
                            android:hint="-  -  -  -  -"
                            android:inputType="number"
                            android:textColor="#516063"
                            android:textColorHint="#b4c8cf"
                            android:textSize="25dp" >
                        </EditText>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.7" />

          ...

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

和以下代码

假设检测到软板开口并向下滚动视图(一次)

    public class PhoneVerifyYourNumbersActivity extends Activity {

        private String mDisplayOptions[] = new String[3];
        private LinearLayout mInputBox;
        private LinearLayout mContinueButton;
        private TextView mVerifyByPhoneCallText;

        private ScrollView mScrollView;

        private EditText mVerificationCodeEditText;
        private String mHash = null;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.phone_verify_your_numbers);

            mHash = getIntent().getStringExtra("Hash");

            initMembers();
            setOnClickListeners();
            initFieldsTexts();
            setKeyboardVisibilityListener();

        }

..

        private void setKeyboardVisibilityListener() {
            final View root = findViewById(R.id.rootLayout);

            root.getViewTreeObserver().addOnGlobalLayoutListener(
                    new OnGlobalLayoutListener() {

                        @Override
                        public void onGlobalLayout() {
                            // TODO Auto-generated method stub

                            int heightDiff = root.getRootView().getHeight()
                                    - root.getHeight();
                            if (heightDiff > 100) { // more than 100 pixels is
                                                    // probably a keyboard
                                // keyboard is shown
                                mInputBox
                                        .setBackgroundResource(R.drawable.input_box_active);
                                //mScrollView.scrollTo(0, mScrollView.getBottom());
                                mScrollView.scrollBy(0, +20);
                            } else {
                                // keyboard is not shown
                                mInputBox
                                        .setBackgroundResource(R.drawable.input_box_idle);
                            }
                        }
                    });
        }
}

}

我的清单:

<activity
    android:name=".phone.PhoneVerifyYourNumbersActivity"
    android:theme="@android:style/Theme.NoTitleBar"
    android:windowSoftInputMode="stateHidden|adjustResize" />

一种奇怪的行为是每当用户将输入插入editText时,

卷轴向下移动1-2 dps。是什么导致这种情况?

我想这mScrollView.scrollBy(0, +20);多次发生

但是我怎么能让它发生一次?

(除了添加布尔标志isMovedAlready之外的任何聪明的想法)?

2 个答案:

答案 0 :(得分:0)

mScrollView.scrollBy()滚动px(像素)而不是dp。你可能想先计算dp到px 但我想你想要一些不同的东西。像这样的东西:

View v = findViewById(R.id.verificationCodeEditText);
mScrollView.scrollTo(0, v.getBottom()+10);//or similar

答案 1 :(得分:0)

// try to add one properties on AndroidManifest.xml Activity declaration like below.
**AndroidManifest.xml** 
<activity
  android:name="PhoneVerifyYourNumbersActivity"
  android:windowSoftInputMode="stateHidden|adjustPan">
</activity>