繁体   English   中英

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

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

以下屏幕截图是我的项目的一部分,灰色列表使用的是listview而不是popupwindow 但是我想实现类似popupwindow的效果,当我单击popupwindow的外部时,弹出窗口将消失。 在此处输入图片说明

为此我能做些什么,请教我,谢谢高级

如果将红色区域的容器设置为诸如LinearLayoutRelativeLayout类的布局填充屏幕,则可以通过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>

以及支持代码:

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