请求焦点不正常

时间:2016-12-23 09:11:00

标签: android request focus

我只是在布局中有一个表单。我只想显示左上角的部分(即单击表单时表单的起始字段。我使用了请求焦点。但是它不起作用。当我打开表单时表单的中间部分进入视图,只有滚动到顶部我才能获得第一个字段。如何解决这个问题,这样当表单打开时我可以到表单的起始部分。这是我的表单布局。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    tools:context="com.example.user.educationhunt.UniversityRegistration">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    </android.support.design.widget.AppBarLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:paddingBottom="22dp"
            android:paddingLeft="22dp"
            android:paddingRight="22dp"
            android:paddingTop="22dp"
            android:orientation="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18dp"
                android:layout_marginBottom="4dp"
                android:text="Name:">
            </TextView>

            <EditText
                android:id="@+id/register_school_name"
                android:layout_width="match_parent"
                android:background="@drawable/rounded_edittext"
                android:padding="11dp"
                android:textSize="12dp"
                android:hint="School Name*"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content">
            </EditText>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18dp"
                android:layout_marginBottom="4dp"
                android:text="Address:"/>

            <EditText
                android:id="@+id/register_school_address"
                android:layout_width="match_parent"
                android:background="@drawable/rounded_edittext"
                android:padding="11dp"
                android:textSize="12dp"
                android:hint="School Address*"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="horizontal">

                <Spinner
                    android:id="@+id/register_school_district"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_marginRight="10dp"
                    android:layout_height="wrap_content" />
                <Spinner
                    android:id="@+id/register_school_country"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_marginBottom="10dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18dp"
                android:layout_marginBottom="4dp"
                android:text="Phone:"/>

            <EditText
                android:id="@+id/register_school_phone"
                android:layout_width="match_parent"
                android:background="@drawable/rounded_edittext"
                android:padding="11dp"
                android:textSize="12dp"
                android:inputType="numberDecimal"
                android:hint="School Phone no*"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18dp"
                android:layout_marginBottom="4dp"
                android:text="Email:"/>

            <EditText
                android:id="@+id/register_school_email"
                android:layout_width="match_parent"
                android:background="@drawable/rounded_edittext"
                android:padding="11dp"
                android:textSize="12dp"
                android:inputType="textEmailAddress"
                android:hint="Valid email address*"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content" />
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18dp"
                android:layout_marginBottom="4dp"
                android:text="Website:"/>

            <EditText
                android:id="@+id/register_school_website"
                android:layout_width="match_parent"
                android:background="@drawable/rounded_edittext"
                android:padding="11dp"
                android:textSize="12dp"
                android:hint="School Website*"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content" />


            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dp"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textSize="18dp"
                    android:layout_marginBottom="4dp"
                    android:text="Institution Type:"/>
                <Spinner
                    android:id="@+id/register_school_institution"
                    android:layout_width="match_parent"
                    android:layout_marginBottom="10dp"
                    android:layout_height="wrap_content" />

            </LinearLayout>
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:textSize="18dp"
                android:text="Establishment Date:"/>

            <EditText
                android:id="@+id/register_school_estdate"
                android:layout_width="match_parent"
                android:background="@drawable/rounded_edittext"
                android:padding="11dp"
                android:textSize="12dp"
                android:hint="School establishment date*"
                android:layout_marginBottom="10dp"
                android:layout_height="wrap_content" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="4dp"
                android:textSize="18dp"
                android:text="Fee Structure:"/>

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">

                    <Spinner
                        android:id="@+id/register_school_level"
                        android:layout_width="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_height="wrap_content" />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">

                    <EditText
                        android:id="@+id/register_school_annualFee"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/rounded_edittext"
                        android:padding="11dp"
                        android:inputType="numberDecimal"
                        android:textSize="12dp"
                        android:hint="Total Annual fee(NRs)*"/>

                </LinearLayout>

            </LinearLayout>

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>
                    <Spinner
                        android:id="@+id/register_school_level1"
                        android:layout_width="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_height="wrap_content" />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>
                    <EditText
                        android:id="@+id/register_school_annualFee1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/rounded_edittext"
                        android:padding="11dp"
                        android:inputType="numberDecimal"
                        android:textSize="12dp"
                        android:hint="1Total Annual fee(NRs)*"/>

                </LinearLayout>

            </LinearLayout>

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>
                    <Spinner
                        android:id="@+id/register_school_level2"
                        android:layout_width="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_height="wrap_content" />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>

                    <EditText
                        android:id="@+id/register_school_annualFee2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/rounded_edittext"
                        android:padding="11dp"
                        android:inputType="numberDecimal"
                        android:textSize="12dp"
                        android:hint="2Total Annual fee(NRs)*"/>

                </LinearLayout>

            </LinearLayout>

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>
                    <Spinner
                        android:id="@+id/register_school_level3"
                        android:layout_width="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_height="wrap_content" />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>

                    <EditText
                        android:id="@+id/register_school_annualFee3"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/rounded_edittext"
                        android:padding="11dp"
                        android:textSize="12dp"
                        android:inputType="numberDecimal"
                        android:hint="3Total Annual fee(NRs)*"/>

                </LinearLayout>

            </LinearLayout>

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>
                    <Spinner
                        android:id="@+id/register_school_level4"
                        android:layout_width="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_height="wrap_content" />
                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>

                    <EditText
                        android:id="@+id/register_school_annualFee4"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/rounded_edittext"
                        android:padding="11dp"
                        android:inputType="numberDecimal"
                        android:textSize="12dp"
                        android:hint="4Total Annual fee(NRs)*"/>

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>
                    <Spinner
                        android:id="@+id/register_school_level5"
                        android:layout_width="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_height="wrap_content" />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>

                    <EditText
                        android:id="@+id/register_school_annualFee5"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/rounded_edittext"
                        android:padding="11dp"
                        android:inputType="numberDecimal"
                        android:textSize="12dp"
                        android:hint="5Total Annual fee(NRs)*"/>

                </LinearLayout>

            </LinearLayout>

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>
                    <Spinner
                        android:id="@+id/register_school_level6"
                        android:layout_width="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_height="wrap_content" />

                </LinearLayout>
                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>

                    <EditText
                        android:id="@+id/register_school_annualFee6"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:background="@drawable/rounded_edittext"
                        android:padding="11dp"
                        android:inputType="numberDecimal"
                        android:textSize="12dp"
                        android:hint="6Total Annual fee(NRs)*"/>

                </LinearLayout>

            </LinearLayout>

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

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="5dp"
                    android:orientation="vertical">
                    <View
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginBottom="7dp"
                        android:background="#6c8487"/>

                    <Spinner
                        android:id="@+id/register_school_level7"
                        android:layout_width="match_parent"
                        android:layout_marginBottom="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_height="wrap_content" />


                </LinearLayout>
        </LinearLayout>  
    </ScrollView>
  </LinearLayout>

1 个答案:

答案 0 :(得分:1)

试试这个:

     Activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
             //focus the view
             edittext.requestFocus();
        });