10

Tôi đang cố gắng sử dụng một số SherlockDialogFragment để hỏi một số đầu vào từ người dùng. Tất cả mọi thứ hoạt động tốt trên điện thoại của tôi (Galaxy Nexus, 4.2), nhưng trên một chiếc điện thoại nhỏ hơn (emulator 2.3.3), khi bàn phím xuất hiện, nó bao gồm hai nút của DialogFragment, như thế này:DialogFragment không thay đổi kích thước khi bàn phím được hiển thị

dialog covered by keyboard

Bố cục của tôi nằm trong ScrollView và tôi đang thay đổi softInputMode thành SOFT_INPUT_ADJUST_RESIZE trên số onViewCreated của mình. Tôi cũng đã cố gắng SOFT_INPUT_ADJUST_PAN, và nó đã không làm việc

MyCustomDialog.java

public class AddTaskDialog extends SherlockDialogFragment implements OnDateSetListener{ 
//... 
    @Override 
    public void onViewCreated(View view, Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 
    } 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     // Use the Builder class for convenient dialog construction 
     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
     this.inflater = getActivity().getLayoutInflater(); 
     View mainView =inflater.inflate(R.layout.custom_dialog, null); 
     builder.setView(mainView); 
     this.taskNote = (EditText) mainView.findViewById(R.id.ET_taskNote); 
     this.taskText = (EditText) mainView.findViewById(R.id.ET_taskText); 
     this.taskValue = (EditText) mainView.findViewById(R.id.ET_taskValue); 
     /* 
     * Other stuff 
     */ 
     builder.setTitle(getString(R.string.new_task, hType.toString())) 
       .setPositiveButton(R.string.dialog_confirm_button, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
        //... 
        } 
       }) 
       .setNegativeButton(R.string.dialog_cancel_button, new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         // User cancelled the dialog 
        } 
       }); 
     // Create the AlertDialog object and return it 
     return builder.create(); 
    } 
} 

Và đây là cách bố trí của tôi:

custom_dialog.xml

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:background="@color/abs__background_holo_light"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="@dimen/activity_vertical_margin" 
     android:paddingRight="@dimen/activity_vertical_margin"> 
     <TextView 
      android:id="@+id/TV_taskText" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/task_text" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
     <EditText 
      android:id="@+id/ET_taskText" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:ems="10" 
      android:hint="@string/create_task_hint" 
      android:inputType="textNoSuggestions" 
      android:singleLine="true" /> 

    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingLeft="@dimen/activity_vertical_margin" 
     android:paddingRight="@dimen/activity_vertical_margin" > 
     <TextView 
      android:id="@+id/TV_taskNote" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/task_note" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <EditText 
      android:id="@+id/ET_taskNote" 
      android:layout_width="0dip" 
      android:layout_height="wrap_content" 
      android:minLines="2" 
      android:layout_weight="1" 
      android:ems="10" 
      android:inputType="textMultiLine" 
      android:hint="@string/task_note_hint"> 

     </EditText> 

    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/repeat_days" 
     android:layout_width="wrap_content" 
     android:layout_height="48dp" 
     android:layout_gravity="top" 
     android:orientation="horizontal" 
     android:visibility="gone" 
     android:paddingLeft="@dimen/activity_vertical_margin" 
     android:paddingRight="@dimen/activity_vertical_margin"> 
     <!-- Day buttons are put here programatically --> 
    </LinearLayout> 
</LinearLayout> 

Vì vậy, bạn có thể giúp tôi và hướng dẫn tôi cách hiển thị các nút đó không? Hoặc để PAN chế độ xem hoặc để thay đổi kích thước ...

Trả lời

21

Đặt thuộc tính windowSoftInputMode thành adjustNothing trong các hoạt động sử dụng đoạn hộp thoại.

<activity 
    ... 
    android:windowSoftInputMode="adjustNothing"> 
... 

onCreateDialog giấu đầu vào mềm:

... 
Dialog dialog = builder.create(); 
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); 
return dialog; 
} 

FYI: https://developer.android.com/training/keyboard-input/visibility.html#ShowOnStart

+6

Vì đó là một DialogFragment, nó không có bất kỳ hoạt động liên quan nào trong tệp kê khai, do đó, điểm đầu tiên là vô dụng, nhưng điểm đầu tiên là cố định nó. Cảm ơn. – MagicMicky

+0

Tôi đã chỉnh sửa câu trả lời của mình để cụ thể hơn về phiên bản của mã kê khai. –

+1

Tại sao không có 'dialog.getWindow(). SetSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); hộp thoại' không được kích thước lại? Điều này trông giống như lỗi. – neworld

26

tôi chỉ sử dụng dòng sau trong DialogFragment tôi:

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

Và không có gì khác, xem ở đây ví dụ đầy đủ:

public class TextEditor extends DialogFragment { 

    public TextEditor() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.fragment_text_editor, container); 

     //set to adjust screen height automatically, when soft keyboard appears on screen 
     getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

     //[add more custom code...] 
     return view; 
    } 
} 
+0

Câu trả lời này phải được chấp nhận –

5

Hãy chắc chắn rằng cách bố trí bên trong một cái nhìn cuộn:

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

    -->your layout here 
</ScrollView> 

và làm theo Dirk comment:

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_text_editor, container); 

//add this line 
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

     //[add more custom code...] 
     return view; 
    } 
+0

Hoàn toàn là điều CHỈ làm việc cho tôi khi cố gắng để có được hành vi 'SOFT_INPUT_ADJUST_PAN'. –

+0

Vâng, đây phải là câu trả lời được chấp nhận nhờ @Sara –

2

Ngoài những thay đổi được đề cập trong câu trả lời khác cũng kiểm tra các chủ đề của dialogfragment
Từ thử nghiệm của tôi, "android: windowIsFloating "thuộc tính dường như ảnh hưởng đến cách cửa sổ phản ứng với đầu vào mềm.

Bạn đặt cài đặt này thành false, cửa sổ sẽ không trượt lên khi bàn phím hiển thị.

2

Mặc dù đã hơi muộn để trả lời, vì câu hỏi nằm trong Hộp thoại, mã sau giải quyết được vấn đề của tôi.

@Override 
public void onCreate(Bundle savedInstanceState) { 
    ... 

    /** Setting STYLE_NO_FRAME allows popup dialog fragment to resize after keyboard is shown */ 
    int style = DialogFragment.STYLE_NO_FRAME, theme = R.style.theme_popupdialog_style; 
    setStyle(style, theme); 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 
    final Dialog dialog = super.onCreateDialog(savedInstanceState); 
    dialog.setCanceledOnTouchOutside(false); 

    dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    dialog.getWindow().setSoftInputMode(WIndowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); 

    return dialog; 
} 

Đối với các chủ đề phong cách, tôi áp dụng đoạn mã sau

/** must put parent="@android:style/Theme.Dialog for it to work */ 
<style name="theme_popupdialog_style" parent="@android:style/Theme.Dialog"> 
    <item .... >...</item> 
</style> 
0

này cũng có thể được gây ra bởi:

<item name="android:windowTranslucentStatus">true</item> 

Cố gắng để xoá bỏ nó từ chủ đề của bạn.