【问题标题】:Android Dialog doesn't go back to bottom when softkeyboard is dismissed关闭软键盘时,Android对话框不会回到底部
【发布时间】:2019-03-08 19:33:28
【问题描述】:

我创建了一个自定义对话框,其中包含从底部显示的 EditText,并在软键盘打开时向上推:

    final Dialog priceFilterDialog = new Dialog(getActivity(), R.style.DialogFilter);

    View priceFilterView = inflater2.inflate(R.layout.view_price_filter, null);
    priceFilterDialog.setContentView(priceFilterView); // your custom view.
    priceFilterDialog.setCancelable(true);
    priceFilterDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    priceFilterDialog.getWindow().setGravity(Gravity.BOTTOM);
    priceFilterDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

问题在于,当用户关闭键盘时,对话框不会返回到底部,而是停留在屏幕中间,由键盘向上推。

我的R.layout.view_price_filter

<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/cl_filter"
    android:focusable="true"
    android:layout_gravity="bottom"
    android:focusableInTouchMode="true">

        <!-- Dialog content here -->

</android.support.constraint.ConstraintLayout>

当键盘关闭时,我应该怎么做才能使对话框返​​回底部?

编辑:请求更多代码:

上面描述的一切都发生在片段SearchFragment中,活动以这种方式调用:

public class MenuActivity extends AppCompatActivity
implements
SearchFragment.OnFragmentInteractionListener,
ProfileFragment.OnFragmentInteractionListener,
ChatsFragment.OnFragmentInteractionListener,
ApplicationsFragment.OnFragmentInteractionListener {

    final Fragment searchFragment = new SearchFragment();
    final Fragment applicationsFragment = new ApplicationsFragment();
    final Fragment chatsFragment = new ChatsFragment();
    final Fragment profileFragment = new ProfileFragment();
    final FragmentManager fm = getSupportFragmentManager();
    Fragment selectedFragment = searchFragment;

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            //Fragment selectedFragment = null;

            switch (item.getItemId()) {
                case R.id.navigation_search:
                    fm.beginTransaction().hide(selectedFragment).show(searchFragment).commit();
                    selectedFragment = searchFragment;
                    return true;
                case R.id.navigation_applications:
                    fm.beginTransaction().hide(selectedFragment).show(applicationsFragment).commit();
                    selectedFragment = applicationsFragment;
                    return true;
                case R.id.navigation_chats:
                    fm.beginTransaction().hide(selectedFragment).show(chatsFragment).commit();
                    selectedFragment = chatsFragment;
                    return true;

                case R.id.navigation_profile:
                    fm.beginTransaction().hide(selectedFragment).show(profileFragment).commit();
                    selectedFragment = profileFragment;
                    return true;

            }
            return false;
        }
    };

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_menu);

    fm.beginTransaction().add(R.id.main_container, profileFragment, "2").hide(profileFragment).commit();
    fm.beginTransaction().add(R.id.main_container, chatsFragment, "3").hide(chatsFragment).commit();
    fm.beginTransaction().add(R.id.main_container, applicationsFragment, "4").hide(applicationsFragment).commit();
    fm.beginTransaction().add(R.id.main_container, searchFragment, "1").commit();

    BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
    navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}

@Override
public void onFragmentInteraction(Uri uri){
}

}

这是片段内部发生的事情:

public class SearchFragment extends Fragment {

private OnFragmentInteractionListener mListener;

public SearchFragment() {
    
}

public static SearchFragment newInstance() {
    SearchFragment fragment = new SearchFragment();
    return fragment;
}

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


}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    final View view = inflater.inflate(R.layout.fragment_search, container, false);

    // Other unrelated code
    
    LayoutInflater inflater2 = this.getLayoutInflater();

    // Price Filter
    final Dialog priceFilterDialog = new Dialog(getActivity(), R.style.DialogFilter);
    
    View priceFilterView = inflater2.inflate(R.layout.view_price_filter, null);
    priceFilterDialog.setContentView(priceFilterView); // your custom view.
    priceFilterDialog.setCancelable(true);
    priceFilterDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    priceFilterDialog.getWindow().setGravity(Gravity.BOTTOM);
    priceFilterDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    
    // Other unrelated code
    
    return view;
}


@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}

public interface OnFragmentInteractionListener {
    void onFragmentInteraction(Uri uri);
}

}

【问题讨论】:

  • 您可以直接从清单中将其设置为...作为活动选项卡的属性,而不是将其设置为对话框窗口!
  • 我在清单活动属性上尝试了android:windowSoftInputMode="adjustPan|adjustResize",但它仍然没有回到底部。

标签: android dialog


【解决方案1】:

您从上面的代码(在干净/纯粹的)应用程序中正常工作(对话框正在关闭):

【讨论】:

  • 我确实尝试过,但它仍然没有回到底部。片段内部发生这种情况会改变什么吗?
  • 如果你能在我上传的代码中找到问题,那对我很有帮助!
  • @Gicminos 你必须上传整个应用程序的某个地方。
猜你喜欢
  • 1970-01-01
  • 2018-02-18
  • 2012-07-22
  • 2011-03-02
  • 1970-01-01
  • 1970-01-01
  • 2019-11-16
  • 2023-03-07
  • 1970-01-01
相关资源
最近更新 更多