简体   繁体   中英

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 . but I want to realize the effect like popupwindow when I click the outside part of popupwindow,the pop will dismiss. 在此处输入图片说明

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

<?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

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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