显示键盘时,全屏 AlertDialog 出现卡顿动画

如何解决显示键盘时,全屏 AlertDialog 出现卡顿动画

在此处观看视频以了解问题:https://streamable.com/s0yhrm

或者在更快的设备上查看:https://streamable.com/4sshdn

我设置了一个 AlertDialog 来占据所有垂直空间。问题是当键盘出现时,随着 AlertDialog 调整大小以适应键盘上方,我会得到一个令人讨厌的动画。有什么办法可以防止这种情况发生吗?理想情况下,它应该在 AlertDialog 显示之前测量键盘上方的可用空间。


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/enter_address_alert_dialog_root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="4dp">

    <TextView
        android:id="@+id/title_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="6dp"
        android:paddingLeft="12dp"
        android:paddingTop="6dp"
        android:paddingBottom="4dp"
        android:text="Delivery address"
        android:textSize="14dp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/network_error_imageView"
        android:layout_alignParentRight="true"
        android:src="@drawable/cloud_off_36dp"
        app:tint="@color/colorError"
        android:visibility="gone"
        tools:visibility="visible"
        android:layout_alignTop="@id/title_textView"
        android:layout_alignBottom="@id/title_textView"
        android:layout_marginRight="12dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:id="@+id/top_LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_below="@id/title_textView"
        android:background="@drawable/autocomplete_list_item_background"
        android:elevation="2dp"
        android:paddingLeft="8dp"
        android:paddingTop="2dp"
        android:paddingRight="8dp"
        android:paddingBottom="12dp">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="bottom">

            <EditText
                android:id="@+id/address_editText"
                style="@style/MyEditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Street address"
                android:imeOptions="actionNext"
                android:maxLength="100"
                android:maxLines="4"
                android:importantForAutofill="no"
                android:paddingLeft="4dp"
                android:paddingTop="8dp"
                android:paddingRight="32dp"
                android:selectAllOnFocus="true"
                android:textSize="@dimen/address_alert_dialog_text_size" />

            <ImageButton
                android:id="@+id/clear_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:paddingTop="4dp"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:paddingBottom="0dp"
                android:background="?selectableItemBackgroundBorderless"
                android:src="@drawable/ic_close_24dp" />

        </RelativeLayout>

        <EditText
            android:id="@+id/enter_address_unit_number_alert_dialog_edit_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="16dp"
            android:layout_weight="0.8"
            android:gravity="center"
            android:imeOptions="actionDone"
            android:hint="Unit #"
            android:importantForAutofill="no"
            android:inputType="textPostalAddress|textCapWords|text"
            android:maxLength="30"
            android:paddingTop="6dp"
            android:selectAllOnFocus="true"
            android:textSize="@dimen/address_alert_dialog_text_size" />

    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/map_button"
        android:layout_below="@id/top_LinearLayout">

        <!--Single parent to ScrollView-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/no_store_address_enter_address_alert_dialog_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:layout_marginTop="@dimen/address_autocomplete_margin_above_empty_view"
                android:layout_marginRight="18dp"
                android:textSize="@dimen/address_alert_dialog_no_store_address_text_size"
                android:text="You haven't entered a store address. Without this,the app can't locate delivery addresses or calculate driving distances. To fix this,go to Settings and use the 'Store addresses' option."
                android:textColor="#F00"
                android:visibility="gone" />

            <TextView
                android:id="@+id/enter_address_alert_dialog_empty_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:layout_marginTop="@dimen/address_autocomplete_margin_above_empty_view"
                android:layout_marginRight="18dp"
                android:drawablePadding="6dp"
                android:gravity="center_horizontal"
                android:padding="12dp"
                android:text="Start typing an address above and matching results will show up here"
                android:textSize="@dimen/address_alert_dialog_text_size"
                app:drawableTint="@color/colorAutocompleteEmptyViewIcon"
                app:drawableTopCompat="@drawable/ic_search_white_28dp" />

        </LinearLayout>

    </ScrollView>

    <ListView
        android:id="@+id/enter_address_alert_dialog_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/map_button"
        android:layout_below="@id/scrollView"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="4dp"
        android:divider="@android:color/transparent"
        android:dividerHeight="-3dp" />

    <TextView
        android:id="@+id/map_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"
        android:layout_marginLeft="16dp"
        android:background="@drawable/toolbar_image_button_ripple"
        android:fontFamily="sans-serif-medium"
        android:padding="8dp"
        android:text="MAP"
        android:textColor="@color/colorAlertDialogButton"
        android:textSize="@dimen/alert_dialog_button_text_size" />

    <TextView
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="15dp"
        android:layout_toLeftOf="@id/submit_button"
        android:background="@drawable/toolbar_image_button_ripple"
        android:fontFamily="sans-serif-medium"
        android:padding="8dp"
        android:text="CANCEL"
        android:textColor="@color/colorAlertDialogButton"
        android:textSize="@dimen/alert_dialog_button_text_size" />

    <TextView
        android:id="@+id/submit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:layout_marginRight="20dp"
        android:background="@drawable/toolbar_image_button_ripple"
        android:fontFamily="sans-serif-medium"
        android:padding="8dp"
        android:text="SUBMIT"
        android:textColor="@color/colorAlertDialogButton"
        android:textSize="@dimen/alert_dialog_button_text_size" />

</RelativeLayout>

解决方法

您的问题没有“默认”解决方法。您的 Dialog 首先出现,然后 EditText 被绘制,它请求焦点,然后键盘显示 - Dialog 已经被绘制(全屏),所以它缩小了

也许您可以尝试在 Dialog 出现之前强制显示键盘

((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).
    toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);

然后显示您的 Dialog(可能会有几毫秒的延迟?)并调用 editText.requestFocus() 以“连接”键盘到新显示的 EditText(有一个更改将自动完成,因为 EditText 在膨胀时可能会请求焦点)

,

仅当用户开始输入时,我才能够通过扩展 AlertDialog 的高度以令人满意的方式解决此问题。在此处观看视频:https://streamable.com/oj3fbv

       addressEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s,int start,int count,int after) {
            }

            @Override
            public void onTextChanged(CharSequence s,int before,int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                String inputText = s.toString().toLowerCase().trim();

                if (inputText.length() > 0) {
                    if (inputText.length() == 1) {
                    autocompleteListView.setVisibility(View.VISIBLE);

                        RelativeLayout.LayoutParams listViewParams = new RelativeLayout.LayoutParams(
                                RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
                        listViewParams.addRule(RelativeLayout.BELOW,topLinearLayout.getId());
                        listViewParams.addRule(RelativeLayout.ABOVE,mapButton.getId());
                        listViewParams.leftMargin = dpToPx(8);
                        listViewParams.rightMargin = dpToPx(8);
                        listViewParams.topMargin = dpToPx(4);
                        listViewParams.bottomMargin = dpToPx(4);
                        autocompleteListView.setLayoutParams(listViewParams);

                        RelativeLayout.LayoutParams mapButtonParams = new RelativeLayout.LayoutParams(
                                RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
                        mapButtonParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                        mapButton.setLayoutParams(mapButtonParams);
                    }

                    clearButton.setVisibility(View.VISIBLE);

                    if (inputText.length() > 1 && storeAddressEntered) {
                        alertDialogEmptyView.setVisibility(View.GONE);
                        scrollView.setVisibility(View.GONE);
                    }
                }
                alertDialogAdapter.getFilter().filter(inputText);
            }
        });
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/enter_address_alert_dialog_root_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="4dp">

    <TextView
        android:id="@+id/title_textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="6dp"
        android:paddingLeft="12dp"
        android:paddingTop="6dp"
        android:paddingBottom="4dp"
        android:text="Delivery address"
        android:textSize="14dp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/network_error_imageView"
        android:layout_alignParentRight="true"
        android:src="@drawable/cloud_off_36dp"
        app:tint="@color/colorError"
        android:visibility="gone"
        tools:visibility="visible"
        android:layout_alignTop="@id/title_textView"
        android:layout_alignBottom="@id/title_textView"
        android:layout_marginRight="12dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:id="@+id/top_LinearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_below="@id/title_textView"
        android:background="@drawable/autocomplete_list_item_background"
        android:elevation="2dp"
        android:paddingLeft="8dp"
        android:paddingTop="2dp"
        android:paddingRight="8dp"
        android:paddingBottom="12dp">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:gravity="bottom">

            <EditText
                android:id="@+id/address_editText"
                style="@style/MyEditTextStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Street address"
                android:imeOptions="actionNext"
                android:maxLength="100"
                android:maxLines="4"
                android:importantForAutofill="no"
                android:paddingLeft="4dp"
                android:paddingTop="8dp"
                android:paddingRight="32dp"
                android:selectAllOnFocus="true"
                android:textSize="@dimen/address_alert_dialog_text_size" />

            <ImageButton
                android:id="@+id/clear_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:paddingTop="4dp"
                android:paddingLeft="4dp"
                android:paddingRight="4dp"
                android:paddingBottom="0dp"
                android:background="?selectableItemBackgroundBorderless"
                android:src="@drawable/ic_close_24dp" />

        </RelativeLayout>

        <EditText
            android:id="@+id/enter_address_unit_number_alert_dialog_edit_text"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:layout_marginLeft="16dp"
            android:layout_weight="0.8"
            android:gravity="center"
            android:imeOptions="actionDone"
            android:hint="Unit #"
            android:importantForAutofill="no"
            android:inputType="textPostalAddress|textCapWords|text"
            android:maxLength="30"
            android:paddingTop="6dp"
            android:selectAllOnFocus="true"
            android:textSize="@dimen/address_alert_dialog_text_size" />

    </LinearLayout>

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/top_LinearLayout">

        <!--Single parent to ScrollView-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:id="@+id/no_store_address_enter_address_alert_dialog_text_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:layout_marginTop="@dimen/address_autocomplete_margin_above_empty_view"
                android:layout_marginRight="18dp"
                android:textSize="@dimen/address_alert_dialog_no_store_address_text_size"
                android:text="You haven't entered a store address. Without this,the app can't locate delivery addresses or calculate driving distances. To fix this,go to Settings and use the 'Store addresses' option."
                android:textColor="#F00"
                android:visibility="gone" />

            <TextView
                android:id="@+id/enter_address_alert_dialog_empty_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="18dp"
                android:layout_marginRight="18dp"
                android:drawablePadding="6dp"
                android:gravity="center_horizontal"
                android:padding="12dp"
                android:text="Start typing an address above and matching results will show up here"
                android:textSize="@dimen/address_alert_dialog_text_size"
                app:drawableTint="@color/colorAutocompleteEmptyViewIcon"
                app:drawableTopCompat="@drawable/ic_search_white_28dp" />

        </LinearLayout>

    </ScrollView>

    <ListView
        android:id="@+id/enter_address_alert_dialog_list_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/scrollView"
        android:visibility="gone"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="4dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="4dp"
        android:divider="@android:color/transparent"
        android:dividerHeight="-3dp" />

    <TextView
        android:id="@+id/map_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/scrollView"
        android:layout_marginLeft="16dp"
        android:background="@drawable/toolbar_image_button_ripple"
        android:fontFamily="sans-serif-medium"
        android:padding="8dp"
        android:text="MAP"
        android:textColor="@color/colorAlertDialogButton"
        android:textSize="@dimen/alert_dialog_button_text_size" />

    <TextView
        android:id="@+id/cancel_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/map_button"
        android:layout_marginRight="15dp"
        android:layout_toLeftOf="@id/submit_button"
        android:background="@drawable/toolbar_image_button_ripple"
        android:fontFamily="sans-serif-medium"
        android:padding="8dp"
        android:text="CANCEL"
        android:textColor="@color/colorAlertDialogButton"
        android:textSize="@dimen/alert_dialog_button_text_size" />

    <TextView
        android:id="@+id/submit_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@id/map_button"
        android:layout_marginRight="20dp"
        android:background="@drawable/toolbar_image_button_ripple"
        android:fontFamily="sans-serif-medium"
        android:padding="8dp"
        android:text="SUBMIT"
        android:textColor="@color/colorAlertDialogButton"
        android:textSize="@dimen/alert_dialog_button_text_size" />

</RelativeLayout>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-