2012-06-20 2 views
15

Я использую DialogFragment. Я хочу, чтобы положительные и отрицательные кнопки оставались над клавиатурой, когда пользователь взаимодействует с текстом редактирования, как на приведенных ниже примерах с экрана в приложении для планшета Gmail.Как сохранить диалогово-позитивные/отрицательные кнопки над мягкой клавиатурой

without soft keyboard

With soft keyboard - buttons remain above keyboard as dialog resizes and content scrolls

В моей попытке, которая не работает, вот мой метод Dialog Фрагменты onCreateDialog

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    LayoutInflater factory = LayoutInflater.from(getActivity()); 
    final View textEntryView = factory.inflate(R.layout.my_layout, null); 

    return new AlertDialog.Builder(getActivity())     
      .setTitle(getString(R.string.paypal)) 
      .setView(textEntryView) 
      .setPositiveButton(R.string.dialog_ok, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) {       

        } 
       } 
      ) 
      .setNegativeButton(R.string.dialog_cancel, 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 

        } 
       } 
      ) 
      .create(); 
} 

и вот R.layout.my_layout

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


    <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> 
     <ImageView android:id="@+id/paypal_button" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="15dip" 
       android:layout_marginBottom="15dip" 

      /> 
     <EditText 
      android:id="@+id/edit_paypal_email" 
       style="@style/GeneralEditText" 
       android:hint="@string/contact_info_text_email" 
       android:inputType="textEmailAddress" 
       /> 

      <View 
      android:id="@id/horizontal_line" 
      style="@style/HorizontalLine" 
      android:layout_marginTop="@dimen/contact_info_line_margin_top" /> 

     <!-- Notes --> 
     <TextView 
      android:text="@string/paypal_info1" 
      style="@style/ContactInfoNotes" 
      android:paddingBottom="5dip" 
      /> 

     <TextView 
      android:text="@string/paypal_info2" 
      style="@style/ContactInfoNotes" 
      android:paddingBottom="5dip" 
      /> 

     <TextView 
      android:text="@string/paypal_info3" 
      style="@style/ContactInfoNotes" 
      android:paddingBottom="5dip" 
      /> 
     <TextView 
      android:text="@string/paypal_info4" 
      style="@style/ContactInfoNotes" 
      android:paddingBottom="5dip" 
      /> 

    </LinearLayout> 
</ScrollView> 

В моей реализации так ft клавиатура подходит и закрывает диалоги положительные и отрицательные кнопки. Что мне не хватает, чтобы кнопки оставались над клавиатурой?

Заранее спасибо.

ответ

26

Просто добавьте это на onViewCreated:

@Override 
public void onViewCreated(View view, Bundle savedInstanceState) { 
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 
    super.onViewCreated(view, savedInstanceState); 
} 
+0

Спасибо! Это сработало для меня по аналогичному сценарию; вместо кнопок. – Karl

+2

Если у вас есть DialogFragment без стандартных кнопок, и вы хотите, чтобы макет диалогового окна прокручивался, макет должен находиться внутри ScrollView и 'getDialog(). GetWindow(). SetSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);' должен быть установлен внутри 'onViewCreated() ' –

0

Я считаю, что это связано с LinearLayout и ScrollView. Удалите часть fillViewport из ScrollView и покажите полосу прокрутки в LinearLayout.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:scrollbars="vertical" 
     android:scrollbarAlwaysDrawVerticalTrack="true"> 
     <!-- Stuff --> 

    </LinearLayout> 
</ScrollView> 
+0

Спасибо, но кнопки по-прежнему скрыты. – Dittimon

0

Вы пытались добавить следующий атрибут к activity тега в манифесте?

<activity android:name=".SampleActivity" 
    android:windowSoftInputMode="adjustResize" 
    ... > 
</activity> 
+0

Спасибо, но это не помешало мягкой клавиатуре закрывать кнопки – Dittimon

3

У меня была такая же проблема, и я нашел решение. Существенная часть файла макета:

<ScrollView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"> 
<!-- Form items --> 
</ScrollView> 

<!-- Include the layout which contains the buttons --> 
<include layout="@layout/fragment_dialog_button_bar" android:layout_weight="0" /> 

Таким образом, вы должны установить кнопки layout_weight 0, в то время как вес Scrollview составляет 1 или больше.

Надеюсь, это поможет.

0

Это то, что сработало для меня.

public class Fragment1 extends DialogFragment{ 
    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState){ 
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 
    super.onViewCreated(view, savedInstanceState); 
    } 
} 


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <ScrollView 
     android:layout_width="match_parent" 
     android:fillViewport="true" 
     android:layout_weight="1" 
     android:layout_height="300dp" > 

     <EditText 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:inputType="textMultiLine|textAutoCorrect|textCapSentences" 
      /> 
    </ScrollView> 

    <Button 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="0" 
     /> 

</LinearLayout>