Android AlertDialog警告对话框实现

本文介绍如何在Android应用中实现警告对话框。通过一个具体的示例展示了如何创建对话框、设置标题、消息以及按钮响应等操作。代码示例中包含了strings.xml、main.xml和MainActivity.java等文件的内容。

今天看了一下Android AlertDialog警告对话框实现相关知识,查询资料自己编写了一个,下面就分享一下

文章来源:好岸园it技术网 http://www.hopean.com

对话框通知主要是当需要用户做出确定或其他某种选择时使用. 贴出代码

strings.xml

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

    <string name="app_name">FileManage</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="button">弹出对话框</string>

</resources>


main.xml

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    
    <Button
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="@string/button"
		android:id="@+id/button"
			/>

</RelativeLayout>


下面是java代码

MainActivity.java

package com.example.filemanage;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.Menu;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		Button button = (Button)findViewById(R.id.button);
		button.setOnClickListener(new View.OnClickListener() {
		@Override
		public void onClick(View v) {
		AlertDialog.Builder builder =
		new
		AlertDialog.Builder(MainActivity.this);
		builder.setTitle("hopean.com")
			.setMessage("你确定要访问 我们网站吗?")
			.setCancelable(false)
			.setPositiveButton("确定",
					new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id)
				{
					//创建一个访问“http://www.hopean.com”网站的意图,
					//该意图会告知系统打开浏览器,并访问该网址。
					Intent intent =
							new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.hopean.com"));
					startActivity(intent);
				}
			})
			.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int id)
				{
					dialog.cancel(); //删除对话框
				}
			});
			AlertDialog alert = builder.create();//创建对话框
			alert.show();//显示对话框
				}
			});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}


源码下载:AlertDialogDemo

 

转载于:https://www.cnblogs.com/hopeanCom/archive/2013/01/05/2858116.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值