设为首页 加入收藏

TOP

Android实现对话框-附一个完整例子(一)
2014-11-23 22:55:09 来源: 作者: 【 】 浏览:22
Tags:Android 实现 对话 一个 完整 例子

新建一个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" >
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_menu_recent"
android:orientation="vertical"
android:padding="2dp" >


android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#333333"
android:orientation="vertical" >


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" />


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" />


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" />




都看懂了没?


显示对话框


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

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Struts2 - View页面中获取Action.. 下一篇如何在Java中执行Hive命令或HiveQL

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: