【问题标题】:How can I click the outside of widget and the widget dismiss?如何单击小部件的外部并关闭小部件?
【发布时间】:2017-08-17 04:22:07
【问题描述】:

以下屏幕截图是我项目的一部分,我使用列表视图而不是popupwindow 的灰名单。 但是我想实现像popupwindow这样的效果,当我点击弹出窗口的外部时,弹出窗口会消失。

我能做什么,请教我,谢谢先进

【问题讨论】:

  • 您希望在用户点击红色标记区域下方时显示一个弹出窗口。
  • 不,我想知道当我点击红色的外部时如何让红色消失

标签: android onclick touch-event


【解决方案1】:

如果您将红色区域的容器设置为诸如LinearLayoutRelativeLayout 之类的布局填充屏幕,那么您可以通过XML 或以编程方式使其可点击并在那里捕获点击。 Here 是如何执行此操作的一个简单示例。

这假设您只想在单击白色区域时关闭。

更新:这是一个简单的例子。如果点击这个小应用程序,它会将白色区域设置为红色。在点击监听器中,您可以轻松地完成消除红色区域所需的操作。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<!--Set onClick and clickable here to capture clicks. -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/backgroundLayout"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clickable="true"
    android:onClick="layoutClicked"
    tools:context="com.example.layout2.MainActivity">

    <!--Set clickable here, too, to capture clicks so they don't propagate
    to underlying view. The button is still enabled, though. -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:background="@android:color/holo_blue_bright"
        android:clickable="true">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>

</LinearLayout>

以及支持代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

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

    public void layoutClicked(View view) {
    // Set the background color of the "outside" area to red.
    // This is where you would dismiss the red area.
        view.setBackgroundColor(0xFFDD0000);
    }
}

【讨论】:

  • 你的意思是在红色区域的父组中添加可点击?我加了不行,我要点击白色部分红色区域关闭
  • 我试试,只有我点击顶部,红色区域会关闭,我点击红色区域的后面,它不会关闭
  • @Jsonzhang 如果单击红色轮廓区域下方的白色区域,onClick 处理程序将获得控制权,您可以关闭onClick 处理程序中的红色区域。如果您问如何关闭ListView,您可以将set its visibility 改为gone
猜你喜欢
  • 1970-01-01
  • 2023-01-04
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2020-05-22
  • 1970-01-01
  • 2019-08-23
  • 2011-11-17
相关资源
最近更新 更多