2016-12-23 2 views
1

У меня просто есть форма внутри макета. Я просто хочу отобразить верхнюю левую часть (т.е. начальное поле формы при нажатии на форму. Я использовал фокус запроса. Однако его не работает. Когда Я открываю форму, в которой отображается средняя часть формы, и только при прокрутке вверх я могу получить первое поле. Как я могу исправить это, чтобы я мог добраться до начальной части формы, когда форма открыта. Это мой . макет формызапрос фокус не работает должным образом

<?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> 
+0

попробуйте запросить фокус на первом EditText формы, которая является одной с id register_school_name. –

+0

Bro Я попробовал это уже.Он не работает – therameshbista

+0

Если вы используете этот макет в setContentView() для деятельности, первый EditText должен автоматически получить фокус. Однако убедитесь, что установлен ScrollView android: focusable = "true" и android: focusableInTouchMode = "true" и попробуйте запросить фокус для первого editText. –

ответ

1

Попробуйте это:

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

где я должен добавить этот код – therameshbista

+0

@therameshbista Вы можете использовать этот блок кода, когда форма открыта. Например, вы можете переопределить метод onStart(), который является одним из жизненного цикла Activity. Если вы хотите, чтобы при щелчке пользователем формы он был сфокусирован, вы можете прослушать событие касания для прокрутки, и там вы также можете использовать блок выше кода. – ziLk

+0

спасибо, что это действительно сработало для меня – therameshbista

Смежные вопросы