简体   繁体   English

如何单击小部件的外部,然后关闭小部件?

[英]How can I click the outside of widget and the widget dismiss?

The following screenshor is my a part of my project, the gray list I use a listview instead of popupwindow . 以下屏幕截图是我的项目的一部分,灰色列表使用的是listview而不是popupwindow but I want to realize the effect like popupwindow when I click the outside part of popupwindow,the pop will dismiss. 但是我想实现类似popupwindow的效果,当我单击popupwindow的外部时,弹出窗口将消失。 在此处输入图片说明

what I can do for that ,please teach me ,thanks advanced 为此我能做些什么,请教我,谢谢高级

If you make the container for the red area a layout such as LinearLayout or RelativeLayout fill the screen, then you can make it clickable through XML or programmatically and capture clicks there. 如果将红色区域的容器设置为诸如LinearLayoutRelativeLayout类的布局填充屏幕,则可以通过XML或以编程方式使其变为可单击状态,并在此捕获点击。 Here is a quick example of how to do this. 是一个简单的例子。

This assumes that you just want to dismiss if the white area is clicked. 这假定您只想单击白色区域即可将其关闭。

Update: Here is a quick example. 更新:这是一个简单的示例。 This little app will set the white area red if it is clicked. 如果单击该小应用程序,它将白色区域设置为红色。 In the click listener, you can easily do what you need to to dismiss the red area. 在点击侦听器中,您可以轻松执行消除红色区域的操作。

activity_main.xml 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>

And the supporting code: 以及支持代码:

MainActivity.java 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);
    }
}

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

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