今天看了一下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