我正在开发一个程序,我想点击一个按钮,屏幕中间会出现一个弹出窗口,显示一个随机图像。然后在单击弹出窗口时关闭弹出窗口。
我的问题是可能的吗?如果是这样的话?
答案 0 :(得分:1)
答案 1 :(得分:0)
是的,您可以使用以下内容:
Dialog confirmDeleteDialog = new Dialog(this);
confirmDeleteDialog.setContentView(R.layout.custom_message_dialog);
//This allows dialog to be closed with back button
confirmDeleteDialog.setCancelable(true);
//DisplayMetrics will get the screen dimensions and allow you to
//declare a center point
DiaplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int screenHeight = displaymetrics.heightPixels; //Divide by 2 to get mid
int screenWidth = displaymetrics.widthPixels; //Divide by 2 to get mid
Button closeButton = (Button) confirmDeleteDialog.findViewById(R.id.close_button);
closeButton.setOnClickListener(this);
confirmDeleteDialog.show();
custom_message_dialog.xml - 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/close_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"/>
</RelativeLayout>
ClickListener
public void onClick(View v) {
if(v == closeButton){
confirmDeleteDialog.dismiss();
}}