展会信息港展会大全

Android中用两层AlertDialog来进行弹出选择框信息选择
来源:互联网   发布日期:2015-10-03 11:29:28   浏览:1697次  

导读:在Android开发中,我们会经常会用到AlertDialog,把内容使用AlertDialog结合列表的形式显示出来,然后我们点击得到点击的信息。这里可以使用两层的AlertDialog来实现弹出选择框信息选择。1:我们现在xml文件中定......

在Android开发中,我们会经常会用到AlertDialog,把内容使用AlertDialog结合列表的形式显示出来,然后我们点击得到点击的信息。这里可以使用两层的AlertDialog来实现弹出选择框信息选择。

1:我们现在xml文件中定义一个要显示内容列表数组

2:在Activity中使用 String[] items = getResources().getStringArray(R.array.item);

3:增添点击事件,使用Alertdialog.builder 千万不能忘了最后进行show()哦

看效果图:

源代码:

package com.jiangqq.alertdialog;

import android.app.Activity;

import android.app.AlertDialog;

import android.content.DialogInterface;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

/**

* 使用AlertDialog进行选择功能

*

* @author jiangqq

*

*/

public class AlertDialogActivity extends Activity {

private Button btn;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

btn = (Button) findViewById(R.id.btn);

btn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

final String[] items = getResources().getStringArray(

R.array.item);

new AlertDialog.Builder(AlertDialogActivity.this)

.setTitle("请点击选择")

.setItems(items, new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog,

int which) {

new AlertDialog.Builder(

AlertDialogActivity.this)

.setTitle("你选择了:" + items[which])

.setMessage("点击选择操作")

.setPositiveButton(

"确定",

new DialogInterface.OnClickListener() {

public void onClick(

DialogInterface dialog,

int which) {

// 这里是你点击确定之后可以进行的操作

}

})

.setNegativeButton(

"取消",

new DialogInterface.OnClickListener() {

public void onClick(

DialogInterface dialog,

int which) {

// 这里点击取消之后可以进行的操作

}

}).show();

}

}).show();

}

});

}

}

string.xml文件内容:

<?xml version="1.0" encoding="utf-8"?>

<resources>

<string name="hello" >Hello World, AlertDialogActivity!</string>

<string name="app_name" >Hello World, AlertDialogActivity</string>

<string name="btn_name" >点击弹出AlertDialog</string>

<string-array name="item" >

<item>第一个选择</item>

<item>第二个选择</item>

<item>第三个选择</item>

<item>第四个选择</item>

</string-array>

</resources>

赞助本站

人工智能实验室

相关热词: AlertDialog 弹出 选择框

相关内容
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港