2012-06-20 87 views
15

我正在使用DialogFragment。当用户使用Gmail平板电脑应用程序屏幕中的下面示例图片与编辑文本进行交互时,我希望正面和负面按钮保持在键盘上方。如何在软键盘上方保留DialogFragment正面/负面按钮

without soft keyboard

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

在我尝试不工作,这是我的对话片段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

谢谢!这对我来说也是类似的情况。 edittext而不是按钮。 – Karl

+2

如果你有没有标准按钮的DialogFragment并且你想让对话框的布局可以滚动,布局必须位于ScrollView和'getDialog().getWindow()。setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);'应该在'onViewCreated ()' –

0

我认为这与LinearLayoutScrollView有关。从ScrollView中移除fillViewport部分,并在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,而滚动型的权重为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> 
相关问题