您的位置:首页 > 移动开发 > Android开发

android实现弹窗和自定义弹窗

2017-03-15 14:04 323 查看
**安卓中弹窗和自定义弹窗**


在安卓中我们有时候要实现弹窗的功能,实现弹窗有很多种实现的方法,有用系统默认的弹窗弹出窗口,但是有很多时候默认的弹窗满足不了我们,在这里我主要讲解两种自定义弹窗的方法和一种默认弹窗效果。

1、 首先我们来讲解一下默认弹窗是怎么实现的,代码如下:

// 创建builder实例

AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setIcon(R.drawable.question_dialog_icon);

builder.setTitle("提示");

builder.setMessage("游戏正在进行,你确定要切换么,切换游戏不会被保存的额?");

// 确定按钮的点击事件

builder.setPositiveButton(R.string.confirm,

newDialogInterface.OnClickListener() {

public void onClick(DialogInterfacedialog,

int whichButton) {

Intentintent = newIntent(JigsawActivity.this,

MainActivity.class);

// 设置退出程序的标志位

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

}

});

// 取消按钮的点击事件

builder.setNegativeButton("取消",

newDialogInterface.OnClickListener() {

@Override

public void onClick(DialogInterfacedialog,

int which) {

}

});

builder.create().show();


2、实现一种自定义弹窗的效果,这种弹窗和上面有点类似,代码如下:

// 设置自定义弹窗

AlertDialog.Builderbuilder;

AlertDialogalertDialog;

// 生成一个inflater对象

LayoutInflaterinflater = LayoutInflater.from(mContext);


// 使用inflater对象根据布局文件生成一个view对象

Viewlayout = inflater.inflate(R.layout.success, null);

// 取出控件

TextViewtxtTextView = (TextView) layout

.findViewById(R.id.mddd);

txtTextView.setText("游戏胜利!");

builder= newAlertDialog.Builder(mContext);

builder.setView(layout);

alertDialog= builder.create();

alertDialog.show();


3、实现自定义弹窗的效果,这种自定义弹窗是通过继承类来实现的,具体代码如下:

//该类用于实现弹出框的效果,实现自定义的功能

public classkeydialog extendsDialog {

// 用来存放代表对话框当中按钮的对象

private final View keys[] = new View[9];

// 存放已经使用过的数字

private int used[];

private shuduview shuduview;

// 构造函数第二个参数当中保存着已经用过的数字

public keydialog(Contextcontext, int[]used, shuduview shuduview) {

super(context);

this.used = used;

this.shuduview = shuduview;


}

@Override

protected void onCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

// 设置标题

setTitle("选择数字");

setContentView(R.layout.key);

findviews();

// 遍历整个数组

for (int i = 0; i < used.length; i++) {

if (used[i] != 0) {

keys[used[i] -1].setVisibility(View.INVISIBLE);

}

}

// 为所有的按钮设置监听器

setlisteners();


}

// 获取全部按钮

private void findviews() {

keys[0] = this.findViewById(R.id.key1);

keys[1] = this.findViewById(R.id.key2);

keys[2] = this.findViewById(R.id.key3);

keys[3] = this.findViewById(R.id.key4);

keys[4] = this.findViewById(R.id.key5);

keys[5] = this.findViewById(R.id.key6);

keys[6] = this.findViewById(R.id.key7);

keys[7] = this.findViewById(R.id.key8);

keys[8] = this.findViewById(R.id.key9);


}

// 通知shuduview对象刷新数据

private void returnresult(int title) {

shuduview.setSelectedtitle(title);

// 取消对话框显示

dismiss();


}

// 设置按钮的监听事件

private void setlisteners() {

// 遍历整个按钮数组

for (int i = 0; i < keys.length; i++) {

final int t = i + 1;

keys[i].setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {

returnresult(t);

}

});

}


}

}

调用上面这种自定义弹窗的方法是:

keydialog keydialog = new keydialog(mContext, usedaleady, this);

keydialog.show();

以上就是这次要实现的弹窗的效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 实例