2015-05-05 4 views
3

У меня есть форма Android, состоящая из некоторых виджета editText и spinner. Когда вы начинаете с первого edittext и нажимаете «next» на экранной клавиатуре, он пропускает прядильщики и переходит прямо к следующему входу edittext. Как я могу предотвратить пропуски записей независимо от того, какие типы записей они есть? то есть, вертушка, EditText, диалог и т.д. Вот мой макет:Как предотвратить андроидную форму от пропусков разделов в форме?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/ScrollView01" android:layout_width="wrap_content" 
android:layout_height="wrap_content" android:scrollbars="vertical" 
android:background="@drawable/flagbackground" > 
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="490dp" 
    android:gravity="center" 
    android:orientation="vertical" > 

<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/vetn" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<EditText android:id="@+id/etVetName" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="text" /> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/vetdob" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<EditText android:id="@+id/etVetDob" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:inputType="date" /> 
<TextView 
    android:layout_width="318dp" 
    android:layout_height="wrap_content" 
    android:text="@string/phone" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText android:id="@+id/etVetPhone" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="top" 
    android:inputType="phone" 
    android:hint="@string/optional" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/branch" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/SpinnerBranchType" 
    android:layout_height="wrap_content" 
    android:prompt="@string/branch" 
    android:layout_width="fill_parent" 
    android:entries="@array/branchtypelist"> 
</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/gender" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/SpinnerSexType" 
    android:layout_height="wrap_content" 
    android:prompt="@string/sex" 
    android:layout_width="fill_parent" 
    android:entries="@array/sexlist"> 
</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/discharge" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/DischargeType" 
    android:layout_height="wrap_content" 
    android:prompt="@string/distype" 
    android:layout_width="fill_parent" 
    android:entries="@array/dischargelist"> 
</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/dd214" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/dd214Response" 
    android:layout_height="wrap_content" 
    android:prompt="@string/dd214" 
    android:layout_width="fill_parent" 
    android:entries="@array/yesnoanswer" > 

</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/home" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/homelessResponse" 
    android:layout_height="wrap_content" 
    android:prompt="@string/homeless" 
    android:layout_width="fill_parent" 
    android:entries="@array/yesnoanswer"> 
</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/weekend" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/ngResponse" 
    android:layout_height="wrap_content" 
    android:prompt="@string/ngrsrv" 
    android:layout_width="fill_parent" 
    android:entries="@array/yesnoanswer"> 
</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/benefit" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/benefitResponse" 
    android:layout_height="wrap_content" 
    android:prompt="@string/benefits" 
    android:layout_width="fill_parent" 
    android:entries="@array/yesnoanswer"> 
</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/foodstamps" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/foodResponse" 
    android:layout_height="wrap_content" 
    android:prompt="@string/food" 
    android:layout_width="fill_parent" 
    android:entries="@array/yesnoanswer"> 
</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/cashaid" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<Spinner 
    android:id="@+id/cashResponse" 
    android:layout_height="wrap_content" 
    android:prompt="@string/cash" 
    android:layout_width="fill_parent" 
    android:entries="@array/yesnoanswer"> 
</Spinner> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/ref" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<EditText android:id="@+id/etRefer" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/refhint" 
    android:inputType="text" /> 
<TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/comment" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
<EditText 
    android:id="@+id/etCommentBody" 
    android:layout_height="wrap_content" 
    android:hint="@string/hint" 
    android:inputType="textMultiLine" 
    android:lines="5" 
    android:layout_width="fill_parent"> 
</EditText> 
<Button android:id="@+id/btnOK" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/submit" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 
</LinearLayout> 

ответ

1

Вы должны обращаться к действию кнопки Далее в себя.

Вот как имитировать щелчок на вашем Spinner, когда пользователь нажимает кнопку Далее на EditText предшествующий Spinner, и выбрав следующую EditText по выбору одного из элементов прядильщика:

// Set the EditText preceding the Spinner's next action to click the Spinner 
editTextBeforeSpinner.setOnEditorActionListener(new TextView.OnEditorActionListener() { 
    @Override 
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
     if (actionId == EditorInfo.IME_ACTION_NEXT) { 
      spinner.performClick(); 
      return true; 
     } 
     return false; 
    } 
}); 

// This is to avoid onItemSelected() being called on setup 
spinner.setSelection(0, false); 

/* Set Spinner's item selection behaviour to move focus to the following View 
    (EditText, other Spinner, etc) on Spinner's item selection */ 
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
    @Override 
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 
     editTextAfterSpinner.requestFocus(); 
    } 

    @Override 
    public void onNothingSelected(AdapterView<?> parent) { 
    } 
}); 

Заметим, что если пользователь повторно выбирает значение, ранее выбранное на Spinner, onItemSelected() не будет вызываться, но есть workarounds.

+0

Благодарим вас за это. Быстрый вопрос, однако, есть ли способ применить это глобально ко всем прядильникам, а не устанавливать это для каждого счетчика отдельно? Я бы хотел, чтобы мой код был минимальным, если можно. Еще раз спасибо. –

+0

Фактор действительно хорош. Переместите код в функции, которые принимают представления как параметры, затем вызовите эти функции на всех ваших прядильщиках. – minipif

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