Android实现对话框-附一个完整例子
Android实现对话框-附一个完整例子
Android其实没有对话框的概念,有的只是用PopupWindow实现一个对话框的效果。下面给一个完整的例子,及用法。
Android中显示AlertDialog对话框
Android学习笔记之具有选择功能的对话框
Android全屏对话框(附带延时关闭效果)
Android自定义登陆窗口-对话框
Android-如何关闭AlertDialog.Builder对话框
新建一个PromptWindow类
package com.friendlocation;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class PromptWindow extends PopupWindow
{
// 列表项
Activity context;
View popupView;
OnBtnClickedListener listener;
public PromptWindow(Activity context, int width, int height)
{
super(context);
this.context = context;
// 创建
popupView = context.getLayoutInflater().inflate(R.layout.prompt_window, null);
Button btnOk = (Button) popupView.findViewById(R.id.btn_ok);
btnOk.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
if(listener!= null)
listener.onBtnOkClicked(PromptWindow.this);
}
});
// 显示对话框
this.setContentView(popupView);
this.setWidth(width);
this.setHeight(height);
setFocusable(true);
setBackgroundDrawable(context.getResources().getDrawable(R.drawable.shape_menu));
//setBackgroundDrawable(new BitmapDrawable());
}
public interface OnBtnClickedListener
{
public void onBtnOkClicked(PopupWindow win);
}
public void setOnBtnClickedListener(OnBtnClickedListener l)
{
listener = l;
}
}
在res/layout下新建一个布局文件 prompt_window.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_menu_recent"
android:orientation="vertical"
android:padding="2dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#333333"
android:orientation="vertical" >
<TextView
android:id="@+id/menu_title"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#555555"
android:gravity="center"
android:text="注册提示"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#EEEEEE"
android:textSize="18sp" />
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:padding="4dp"
android:text="软件未注册时限制最多100条信息,请删除若干消息后继续使用。或购买注册码。"
android:textColor="#DDDDDD"
android:textSize="14sp" />
<Button
android:id="@+id/btn_ok"
style="?android:attr/buttonStyleSmall"
android:layout_width="100dp"
android:layout_height="28dp"
android:layout_gravity="right"
android:layout_margin="4dp"
android:background="@drawable/toolbutton"
android:text="点此注册"
android:textColor="#DDDDDD" />
</LinearLayout>
</LinearLayout>
都看懂了没?
显示对话框
PromptWindow win = new PromptWindow(this, 420, 240);
win.showAtLocation(this.findViewById(R.id.titlebar), Gravity.CENTER, 0, 0);
// 点击按钮时去干什么
win.setOnBtnClickedListener(new PromptWindow.OnBtnClickedListener()
{
public void onBtnOkClicked(PopupWindow w)
{
w.dismiss();
//
Intent intent = new Intent(MainActivity.this, RegisterActivity.class);
startActivityForResult(intent, 100);
}
});
评论暂时关闭