Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

java - Can not get ScrollView to scroll when soft keyboard is shown

I have been trying to get this scrollview to scroll, and have not had any luck in doing so. I have tried many types of fixes but cant seem to get one working. I would like to be able to scroll through my page when an edit box is selected and the keyboard takes up half the screen. This way i can navigate to the rest of the edit boxes and the accept button on the bottom of the page, with out having to manually hide the keyboard every time.

Here is my xml code:

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:background="@drawable/asuswallpaper4"
       android:orientation="vertical"
       android:weightSum="1"
      >



        <TextView
          android:id="@+id/textView1"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="#AAA"
          android:text="Input Configuration"
          android:textAppearance="?android:attr/textAppearanceLarge"
          android:textColor="#000" >
         </TextView>

             <ScrollView
               android:id="@+id/input_scroll_view"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content" >

               <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >



                      <LinearLayout
                       android:id="@+id/linearLayout10"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       android:layout_marginTop="20dp"
                       android:orientation="horizontal" >

                      <TextView
                       android:id="@+id/textView13"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:text="TextView"
                       android:textAppearance="?android:attr/textAppearanceMedium"
                       android:textColor="#000"
                       android:textStyle="bold" >
                      </TextView>

            <TextView
                android:id="@+id/textView11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Enable or Disable"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF" >
            </TextView>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp" >

            <RadioGroup
                android:id="@+id/DisableEnable"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:addStatesFromChildren="true"
                android:orientation="horizontal" >

                <RadioButton
                    android:id="@+id/disableButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="Disable"
                    android:textColor="#FFF" >
                </RadioButton>

                <RadioButton
                    android:id="@+id/enableButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="100dp"
                    android:text="Enable"
                    android:textColor="#FFF" >
                </RadioButton>
            </RadioGroup>
        </LinearLayout>

        <!-- <LinearLayout -->
        <!-- android:id="@+id/linearLayout3" -->
        <!-- android:layout_width="match_parent" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:layout_marginTop="20dp" -->
        <!-- android:orientation="horizontal" > -->


        <!-- <TextView -->
        <!-- android:id="@+id/textView14" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:text="TextView" -->
        <!-- android:textAppearance="?android:attr/textAppearanceMedium" -->
        <!-- android:textColor="#000" -->
        <!-- android:textStyle="bold" -->
        <!-- android:visibility="invisible" > -->
        <!-- </TextView> -->


        <!-- <TextView -->
        <!-- android:id="@+id/textView3" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:text="Positive/Negative Trigger" -->
        <!-- android:textAppearance="?android:attr/textAppearanceMedium" -->
        <!-- android:textColor="#FFF" -->
        <!-- android:visibility="invisible" > -->
        <!-- </TextView> -->
        <!-- </LinearLayout> -->


        <!-- <LinearLayout -->
        <!-- android:id="@+id/linearLayout4" -->
        <!-- android:layout_width="match_parent" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:orientation="vertical" > -->


        <!-- <RadioGroup -->
        <!-- android:id="@+id/PosNeg" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:orientation="horizontal" -->
        <!-- android:visibility="invisible" android:clickable="false"> -->


        <!-- <RadioButton -->
        <!-- android:id="@+id/positive_trigger" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:checked="true" -->
        <!-- android:text="Positive" -->
        <!-- android:textColor="#FFF" android:clickable="false"> -->
        <!-- </RadioButton> -->


        <!-- <RadioButton -->
        <!-- android:id="@+id/negative_trigger" -->
        <!-- android:layout_width="wrap_content" -->
        <!-- android:layout_height="wrap_content" -->
        <!-- android:layout_marginLeft="100dp" -->
        <!-- android:text="Negative" -->
        <!-- android:textColor="#FFF" android:clickable="false"> -->
        <!-- </RadioButton> -->
        <!-- </RadioGroup> -->
        <!-- </LinearLayout> -->

        <LinearLayout
            android:id="@+id/linearLayout5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/textView15"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#000"
                android:textStyle="bold"
                android:visibility="invisible" >
            </TextView>

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Type of Trigger"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="#FFF"
                android:visibility="invisible" >
            </TextView>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <RadioGroup
                android:id="@+id/TypeTrigger"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:visibility="invisible" >

                <RadioButton
                    android:id="@+id/constant_trigger"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="Constant"
                    android:textColor="#FFF"
                    android:visibility="visible" >
                </RadioButton>

                <RadioButton
                    android:id="@+id/pulse_trigger"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="100dp"
                    android:text="Pulse"
                    android:textColor="#FFF" >
                </RadioButton>
            </RadioGroup>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="25dp" >

            <TextView
                android:id="@+id/textView16"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textStyle="bold"
                android:visibility="invisible" />

            <TextView
                android:id="@+id/textView17"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text=": Continuous or One Time (One Time Only Available for                         Pulsed Input(s))"  
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:visibility="invisible" />
            </LinearLayout>

            <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RadioGroup
                android:id="@+id/contOneTime"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:visibility="invisible" >

                <RadioButton
                    android:id="@+id/continuous_trigger"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="true"
                    android:text="Continuous" />

                <RadioButton
                    android:id="@+id/oneTime_trigger"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="80dp"
            

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Add android:windowSoftInputMode="adjustResize" to the <activity> tag in your AndroidManifest.xml. This will cause the screen to be resized to the space left over after the software keyboard is shown. As a result, you will be able to scroll, since the screen will not be covered by the keyboard in any way.

EDIT:

I have written a minimal example and tested it. Unless there is a huge misunderstanding, try this code and then figure out why yours doesn't work:

xml layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >

        <TextView
            android:layout_height="2000dp"
            android:layout_width="wrap_content"
            android:gravity="top"
            android:text="Scroll Down!"/>

        <EditText
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:hint="Enter Text"
            />
    </LinearLayout>
</ScrollView>

manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.example"
          android:versionCode="1"
          android:versionName="1.0">
    <uses-sdk android:minSdkVersion="15"/>
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="MyActivity"
                  android:label="@string/app_name"
                android:windowSoftInputMode="adjustResize"
                >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...