【问题标题】:How to keep DialogFragment positive/negative buttons above soft keyboard如何将 DialogFragment 正/负按钮保留在软键盘上方
【发布时间】:2012-06-20 00:07:42
【问题描述】:

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

在我的尝试中不起作用,这是我的 Dialog Fragments 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>

在我的实现中,软键盘出现并覆盖了对话框的正面和负面按钮。为了让按钮保留在键盘上方,我缺少什么?

提前致谢。

【问题讨论】:

    标签: android android-dialogfragment


    【解决方案1】:

    只需将其添加到您的 onViewCreated 中:

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

    【讨论】:

    • 谢谢!这在类似的情况下对我有用;编辑文本而不是按钮。
    • 如果您的 DialogFragment 没有标准按钮,并且您想让对话框的布局可滚动,则布局必须位于 ScrollView 内,getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); 应设置在 onViewCreated()
    【解决方案2】:

    我遇到了同样的问题,我找到了解决方案。布局文件的基本部分:

    <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 或更大。

    我希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      我相信这与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>
      

      【讨论】:

      • 谢谢,但按钮仍然隐藏。
      【解决方案4】:

      您是否尝试将以下属性添加到清单中的 activity 标记?

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

      【讨论】:

      • 谢谢,但这并没有阻止软键盘覆盖按钮
      【解决方案5】:

      这对我有用。

      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>
      

      【讨论】:

        猜你喜欢
        • 2013-02-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-07
        • 2019-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多