我在我的弹出窗口里面点击,它正在关闭

如何解决我在我的弹出窗口里面点击,它正在关闭

下午好,我在 popupview 内部点击,它正在关闭,我希望的行为是点击事件只会通过点击外部触发,但是正如你所看到的,弹出窗口占据了整个屏幕,是问题?

如您所见,弹出窗口占据了整个屏幕,背景是透明的,但是如果我在弹出窗口内部单击它会关闭 popup

如何使用 ConstraintLayout 将弹出窗口居中?

代码弹出

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:background="#80AAAAAA"
android:layout_margin="40dp"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
    android:layout_margin="20dp"
    android:id="@+id/pop_up_enviar_ocorrencias_card_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    app:cardCornerRadius="10dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintVertical_bias="0.0"
    android:padding="20dp"
    app:layout_constraintHorizontal_bias="0.0">
    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/pop_up_ocorrencias_constraint_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="8dp">
        <TextView
            android:id="@+id/pop_up_ocorrencias_txt_cracha"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="marcações"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.0" />
        <ListView
            android:id="@+id/pop_up_enviar_ocorrencias_list_marcacoes"
            android:layout_width="0dp"
            tools:listitem="@layout/item_marcacao"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="@id/pop_up_ocorrencias_txt_cracha"
            app:layout_constraintTop_toBottomOf="@id/pop_up_ocorrencias_txt_cracha"/>
        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Ocorrências"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_list_marcacoes"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_list_marcacoes" />
        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias" />
        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_motivo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Motivo"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_ocorrencias_api" />
        <EditText
            android:id="@+id/pop_up_enviar_ocorrencias_edt_motivos_api"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Motivo"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_motivo"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_motivo" />

        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_descricao"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Descrição"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_edt_motivos_api"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_edt_motivos_api" />
        <EditText
            android:id="@+id/pop_up_enviar_ocorrencias_edt_descricao_api"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Descrição da ocorrência"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_descricao"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_descricao" />

        <TextView
            android:id="@+id/pop_up_enviar_ocorrencias_txt_foto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Foto"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_edt_descricao_api"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_edt_descricao_api" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/pop_up_enviar_ocorrencias_constraint_foto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_txt_foto"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_txt_foto">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/pop_up_enviar_ocorrencias_txt_selecione_imagem"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:text="Selecione uma imagem"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/pop_up_enviar_ocorrencias_image_selecione_imagem"
                app:layout_constraintStart_toStartOf="parent" />

            <ImageView
                android:id="@+id/pop_up_enviar_ocorrencias_image_selecione_imagem"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_cselect_image"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_constraint_foto"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_constraint_foto">

            <androidx.appcompat.widget.AppCompatTextView
                android:id="@+id/pop_up_enviar_ocorrencias_txt_vizualizar_foto"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:breakStrategy="high_quality"
                android:text="Vizualizar Foto"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent" />

            <ImageView
                android:id="@+id/pop_up_enviar_ocorrencias_image_vizualizar_foto"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:src="@drawable/ic_arrow_down"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
        <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/pop_up_enviar_ocorrencias_enviar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Enviar"
            android:background="@drawable/button_round"
            app:layout_constraintStart_toStartOf="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto"
            app:layout_constraintTop_toBottomOf="@+id/pop_up_enviar_ocorrencias_constraint_foto_vizualizar_foto" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

编码我的类弹出

public class PopUpOcorrencia {

//referencia https://medium.com/@evanbishop/popupwindow-in-android-tutorial-6e5a18f49cc7
@RequiresApi(api = Build.VERSION_CODES.Q)
@SuppressLint("ClickableViewAccessibility")
public void showPopupWindow(final View view){
    //Crie você mesmo um objeto View através do inflater
    LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View popupView = inflater.inflate(R.layout.pop_up_enviar_ocorrencia,null);

    // Especifique o comprimento e a largura por meio de constantes
    int width = ConstraintLayout.LayoutParams.MATCH_PARENT;
    int height = ConstraintLayout.LayoutParams.MATCH_PARENT;

    // Tornar itens inativos fora da PopupWindow
    boolean focusable = true;

    // Crie uma janela com nossos parâmetros
    final PopupWindow popupWindow = new PopupWindow(popupView,width,height,focusable);

    // Defina a localização da janela na tela
    popupWindow.showAtLocation(view,Gravity.CENTER,0);

    popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#80AAAAAA")));
    popupWindow.setFocusable(true);
    popupWindow.setoutsidetouchable(true);

    //popupWindow.setTouchModal(true);

    // Inicialize os elementos de nossa janela,instale o manipulador
    TextView test2 = popupView.findViewById(R.id.pop_up_enviar_ocorrencias_txt_ocorrencias_api);
    test2.setText("Teste");

    Button buttonEdit = popupView.findViewById(R.id.pop_up_enviar_ocorrencias_enviar);
    buttonEdit.setonClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            //As an example,display the message
            Toast.makeText(view.getContext(),"Wow,popup action button",Toast.LENGTH_SHORT).show();

        }
    });

    // Manipulador para clicar na zona inativa da janela
    popupView.setonTouchListener((View v,MotionEvent event) -> {

        //Close the window when clicked
        popupWindow.dismiss();
        return true;
    });
  }
}

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

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?