当键盘在对话框片段中打开时它会隐藏我的按钮

时间:2016-09-11 09:16:13

标签: android keyboard android-dialogfragment

在我的应用中,我打开dialog fragment。在dialog fragment我有2个editText和4个Buttons,其中一个在我关闭对话框和其他3个下方。当我按下编辑按钮时,键盘显示并向上推editText但隐藏3 buttons。 这是dialog fragment的2个打印屏幕。

  1. 打印屏幕是我打开dialog时,这很好,就像我想要的那样,dialog打开,键盘无法打开。
  2. enter image description here

    1. 在此打印屏幕中是问题所在。我点击编辑按钮,键盘打开我的EditText,但它隐藏了所有3 buttons,就像在图片上一样。
    2. enter image description here

      这是我的对话框XML代码:

      <?xml version="1.0" encoding="utf-8"?>
      
      <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:background="@color/bg_dialog">
      
          <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <ImageButton
                  android:id="@+id/btn_close"
                  android:layout_width="16dp"
                  android:layout_height="16dp"
                  android:layout_alignParentRight="true"
                  android:background="@null"
                  android:scaleType="fitCenter"
                  android:src="@drawable/ic_close" />
      
              <EditText
                  android:id="@+id/dialog_text_naslov"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_below="@id/btn_close"
                  android:paddingTop="5dp"
                  android:paddingBottom="5dp"
                  android:background="@drawable/bg_et_drop"
                  android:gravity="center"
                   />
      
              <EditText
                  android:id="@+id/dialog_text_note"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/dialog_text_naslov"
                  android:layout_marginTop="20dp"
                  android:background="@drawable/bg_et_drop"
                  android:paddingTop="5dp"
                  android:paddingBottom="5dp"
                  android:gravity="left"
                  android:paddingLeft="5dp"
                 />
      
              <Button
                  android:id="@+id/dialog_btn_edit"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@id/dialog_text_note"
                  android:background="@null"
                  android:drawablePadding="5dp"
                  android:drawableTop="@drawable/ic_action_edit"
                  android:text="Edit"
                  android:textAllCaps="false" />
      
              <Button
                  android:id="@+id/dialog_btn_completed"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@id/dialog_text_note"
                  android:layout_centerHorizontal="true"
                  android:background="@null"
                  android:drawablePadding="5dp"
                  android:drawableTop="@drawable/ic_action_completed"
                  android:text="Mark As Completed"
                  android:textAllCaps="false" />
      
              <Button
                  android:id="@+id/dialog_btn_save"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignParentRight="true"
                  android:layout_below="@id/dialog_text_note"
                  android:background="@null"
                  android:drawablePadding="5dp"
                  android:drawableTop="@drawable/ic_action_save"
                  android:text="Save"
                  android:textAllCaps="false" />
      
          </RelativeLayout>
      
      </ScrollView>

      这是我的Dialog fragment完整的java代码,但我用键盘做的一切都是当我点击编辑button时,在这部分中,点击发生后我调用方法editDialogNoteText();我打开键盘:

         case R.id.dialog_btn_edit:
                      Toast.makeText(getActivity(), "Edit pressed", Toast.LENGTH_SHORT).show();
                      editDialogNoteText();
                      break;

      import android.content.Context;
      import android.os.Bundle;
      import android.support.annotation.Nullable;
      import android.support.v4.app.DialogFragment;
      import android.view.LayoutInflater;
      import android.view.View;
      import android.view.ViewGroup;
      import android.view.WindowManager;
      import android.view.inputmethod.InputMethodManager;
      import android.widget.Button;
      import android.widget.EditText;
      import android.widget.ImageButton;
      import android.widget.Toast;
      
      import com.petar.android.simplenote.adapters.ChangeNoteListener;
      import com.petar.android.simplenote.beans.Drop;
      
      import io.realm.Realm;
      import io.realm.RealmResults;
      
      /**
       * Created by Petar_K on 8/31/2016.
       */
      public class DialogNote extends DialogFragment implements View.OnClickListener {
      
          private ImageButton mBtnClose;
          private EditText mEditTextTitle;
          private EditText mEditTextNote;
          private Button mBtnEdit;
          private Button mBtnCompleted;
          private Button mBtnSave;
          private ChangeNoteListener mNotelistener;
      
          private RealmResults<Drop> mRealmResolts;
          private Realm mRealm;
          private RealmResults<Drop> mRealmResolts2;
      
          @Override
          public void onCreate(@Nullable Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogTheme); //adding a theme
          }
      
          @Nullable
          @Override
          public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
              return inflater.inflate(R.layout.dialog_note, container, false);
          }
      
          @Override
          public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
              super.onViewCreated(view, savedInstanceState);
      
              mBtnClose = (ImageButton) view.findViewById(R.id.btn_close);
              mEditTextTitle = (EditText) view.findViewById(R.id.dialog_text_naslov);
              mEditTextNote = (EditText) view.findViewById(R.id.dialog_text_note);
              mBtnEdit = (Button) view.findViewById(R.id.dialog_btn_edit);
              mBtnCompleted = (Button) view.findViewById(R.id.dialog_btn_completed);
              mBtnSave = (Button) view.findViewById(R.id.dialog_btn_save);
      
              mBtnClose.setOnClickListener(this);
              mBtnEdit.setOnClickListener(this);
              mBtnCompleted.setOnClickListener(this);
              mBtnSave.setOnClickListener(this);
      
              Realm mRealm2 = Realm.getDefaultInstance();
              mRealmResolts2 = mRealm2.where(Drop.class).findAllAsync();
      
              setTitleNoteTextToDialogNote();
      
      
          }
      
          private void setTitleNoteTextToDialogNote() {
              mEditTextNote.setEnabled(false);
              mEditTextTitle.setEnabled(false);
              Bundle argumetns = getArguments();
              int position = argumetns.getInt("POSITION");
      
              mEditTextTitle.setText(mRealmResolts2.get(position).getWhat());
              mEditTextNote.setText(mRealmResolts2.get(position).getWhat_note());
      
          }
      
          @Override
          public void onClick(View view) {
              int id = view.getId();
      
              switch (id) {
                  case R.id.btn_close:
                      Toast.makeText(getActivity(), "Closse pressed", Toast.LENGTH_SHORT).show();
                      dismiss();
                      break;
                  case R.id.dialog_btn_edit:
                      Toast.makeText(getActivity(), "Edit pressed", Toast.LENGTH_SHORT).show();
                      editDialogNoteText();
                      break;
                  case R.id.dialog_btn_completed:
                      markAsCompleted();
                      dismiss();
                      break;
                  case R.id.dialog_btn_save:
                      saveDialogNoteText();
                      Toast.makeText(getActivity(), "Save pressed", Toast.LENGTH_SHORT).show();
                      break;
      
              }
      
          }
      
          private void saveDialogNoteText() {
              Bundle argumetns = getArguments();
              int position = argumetns.getInt("POSITION");
              String text = mEditTextNote.getText().toString();
      
              Realm realm = Realm.getDefaultInstance();
              mRealmResolts2 = realm.where(Drop.class).findAllAsync();
              realm.beginTransaction();
              realm.copyToRealm(mRealmResolts2.get(position)).setWhat_note(text);
              // mEditTextTitle.setText(mRealmResolts.get(position).getWhat());
              realm.commitTransaction();
              mEditTextNote.setText(text);
              mEditTextNote.setEnabled(false);
          }
      
          private void editDialogNoteText() {
              mEditTextNote.setEnabled(true);
              int pos = mEditTextNote.getText().length();
              mEditTextNote.requestFocus();
              mEditTextNote.setSelection(pos);
      
      
              InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
              imm.showSoftInput(mEditTextNote, InputMethodManager.SHOW_IMPLICIT);
      
              getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
      
      
      
      
          }
      
          private void markAsCompleted() {
              Bundle argumetns = getArguments();
              if (mNotelistener != null && argumetns != null) { 
                  int position = argumetns.getInt("POSITION");
                  mNotelistener.onComplete(position);
      
              }
          }
      
          public void setChangeNoteListener(ChangeNoteListener mChangeNoteListener, RealmResults<Drop> realmResults, Realm realm) {
              mNotelistener = mChangeNoteListener;
              mRealmResolts = realmResults; // not used 
              mRealm = realm; //not used 
          }
      
      
      }

1 个答案:

答案 0 :(得分:0)

我猜,但是我想,只要你从软键盘输入 EditText 你想要的东西,然后按返回,就可以了键盘将消失,您的按钮将重新出现。:)

另外,为什么你的软键盘设置

WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE 

而不是

WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE