Android悬浮对话框(即点即关对话框)实现代码

2019-12-10 18:35:22王旭

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://www.easck.com/apk/res/android"
package="net.blogjava.mobile"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/date" android:label="@string/app_name">
<activity android:name=".Main" android:label="@string/app_name" android:theme="@android:style/Theme.Dialog">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="这是一个悬浮对话框"
android:layout_marginLeft="20dp" android:layout_marginRight="20dp" />
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:gravity="center"
android:layout_marginTop="20dp">
<Button android:id="@+id/btnCurrentDate"
android:layout_width="100dp" android:layout_height="wrap_content"
android:text="当前日期" />
<Button android:id="@+id/btnFinish" android:layout_width="80dp"
android:layout_height="wrap_content" android:text="关闭" />
</LinearLayout>
</LinearLayout>

这两个按钮的单击事件代码如下:

public void onClick(View view)
{
switch (view.getId())
{
case R.id.btnCurrentDate:
// 显示当前日期对话框
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
dateDialog.setIcon(R.drawable.date);
dateDialog.setTitle("当前日期:"
+ simpleDateFormat.format(new Date()));
dateDialog.setButton("确定", new OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
}
});
dateDialog.setOnDismissListener(new OnDismissListener()
{
@Override
public void onDismiss(DialogInterface dialog)
{
new DateDialog.Builder(Main.this).setMessage(
"您已经关闭的当前对话框.").create().show();
}
});
dateDialog.show();
break;
case R.id.btnFinish:
// 关闭悬浮Activity
finish();
break;
}
}