[英]How can I click the outside of widget and the widget dismiss?
如果将红色区域的容器设置为诸如LinearLayout
或RelativeLayout
类的布局填充屏幕,则可以通过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.