简体   繁体   English

输入长文本后,带有自定义视图的AlertDialog在软键盘后面消失

[英]AlertDialog with custom view disappears behind Soft Keyboard after entering a long text

I have a simple AlertDialog with this custom View : 我有一个带有此自定义View的简单AlertDialog

在此处输入图片说明

XML XML格式

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="20dp"
    android:layout_alignParentTop="true">

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter a comment"
    android:linksClickable="true"
    android:background="@android:color/transparent"
    android:inputType="textMultiLine"
    android:scrollbars="vertical"/>

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"

    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <ImageView
            android:id="@+id/imageview_bold"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_margin="10dp"
            android:src="@drawable/bold_b" />

        <ImageView
            android:id="@+id/imageview_italic"
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:layout_margin="10dp"
            android:src="@drawable/italic_i" />


        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:id="@+id/imageview_underlined"
            android:layout_margin="10dp"
            android:src="@drawable/underlined_u"/>


        <ImageView
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:id="@+id/add_link"
            android:layout_margin="10dp"
            android:src="@mipmap/ic_launcher_link"/>


    </LinearLayout>

</HorizontalScrollView>

</LinearLayout>

Problem 问题

A long text causes the AlertDialog to hide behind Soft Keyboard. 较长的文本会导致AlertDialog隐藏在软键盘后面。 在此处输入图片说明

And the FormattingBar ( B I U Link) is also hidden and only visible with a short text. 而且FormattingBarB I U链接)也被隐藏,并且只有短文本可见。

The AlertDialog should always stay over the Soft Keyboard and if the text is too long, the EditText has to be scrollable, but the FormattingBar and the AlertDialog buttons should always be visible. AlertDialog应始终停留在软键盘上,如果文本太长,则EditText必须可滚动,但FormattingBarAlertDialog按钮应始终可见。

在此处输入图片说明

How can I do this? 我怎样才能做到这一点?

I already tried stuff like 我已经尝试过类似的东西

android:fitsSystemWindows="true"

android:windowSoftInputMode="adjustResize"

android:windowSoftInputMode="adjustPan"

but nothing worked. 但没有任何效果。

Using maxLines causes problems with different devices. 使用maxLines会导致不同设备出现问题。 The number of maxlines varies. maxlines的数量各不相同。

You need to use setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) to your AlertDialog 您需要对您的AlertDialog使用setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)

SAMPLE CODE 样本代码

Activity 活动

import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.WindowManager
import androidx.appcompat.app.AlertDialog

class MainActivity : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        showAlertDialog(this)

    }

    private fun showAlertDialog(
            context: Context
    ) {
        val dialogBuilder = AlertDialog.Builder(context)

        dialogBuilder.setView(R.layout.aa)

        val dialog = dialogBuilder.create()
        dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
        dialog.show()
    }

}

dialog layout file 对话框布局文件

<?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="wrap_content"
    android:fillViewport="true"
    android:orientation="vertical"
    android:scrollbars="vertical"
    android:padding="20dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="20dp">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:hint="Enter a comment"
            android:inputType="textMultiLine"
            android:linksClickable="true"
            android:scrollbars="vertical" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/imageview_bold"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher_round" />

            <ImageView
                android:id="@+id/imageview_italic"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher_round" />


            <ImageView
                android:id="@+id/imageview_underlined"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher_round" />


            <ImageView
                android:id="@+id/add_link"
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher_round" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="horizontal">

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher_round" />

            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher_round" />


            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher_round" />


            <ImageView
                android:layout_width="35dp"
                android:layout_height="35dp"
                android:layout_margin="10dp"
                android:src="@mipmap/ic_launcher_round" />


        </LinearLayout>

    </LinearLayout>

</ScrollView>

Your issue is inputType 您的问题是inputType

Remove that line and try this code instead 删除该行,然后尝试使用此代码

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter a comment"
    android:background="@android:color/transparent"
    android:maxHeight="90dp"/>

Update : 更新

edittext.setScroller(new Scroller(myContext));
edittext.setMaxLines(5);
edittext.setVerticalScrollBarEnabled(true);
edittext.setMovementMethod(new ScrollingMovementMethod());

By using this method you can get the keyboard dynamic height and you can set the edittext static height % based on the total height of the screen 通过使用此方法,您可以获得键盘的动态高度,并且可以基于屏幕的总高度来设置edittext静态高度%

root.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
     public void onGlobalLayout(){
           int heightDiff = root.getRootView().getHeight()- root.getHeight();
           // IF height diff is more then 150, consider keyboard as visible.  
        }
  });

this only works when android:windowSoftInputMode="adjustResize" 仅在android:windowSoftInputMode="adjustResize"

Try this solution: 试试这个解决方案:

  1. Get the height of the device by adding the following method to your activity class 通过将以下方法添加到活动类中来获取设备的高度

     public int getHeight(){ DisplayMetrics displayMetrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int height = displayMetrics.heightPixels; return height; } 
  2. Set the MaxHeight according to the device height so it won't cause any problem on bigger devices like tablets by adding this block of code to your onCreate() method. 通过将此代码块添加到onCreate()方法中,根据设备高度设置MaxHeight,这样在较大的设备(如平板电脑)上不会引起任何问题。

      EditText editText = findViewById(R.id.editText); //The EditText in the Layout editText.setMaxHeight(getHeight()/3); // 

The XML is ok but don't forget to add the id to the editText XML可以,但不要忘记将id添加到editText

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM