对话框是程序运行时弹出的窗口,AlertDialog是一个提示窗口,要求用户作出相应的选择,
今天学习利用AlertDialog完成一个例子,并结合上一个例子在一起。
在程序中创建对话框的步骤如下:
1、获得AlertDialog的内部类Builder的对象。
2、通过Builder对象来设置标题、按钮名称等。
3、调用Builder 的create()方法来创建对话框。
4、调用show()方法来显示对话框。
dialog.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/username"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="账号"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/username"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/password"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="密码"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:password="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
AlertDialog.java代码如下:
package com.liu;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.GridView;
public class Alertdialog extends Activity
{
Dialog dialog ;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);
//设置背景
GridView gridview = (GridView)findViewById(R.id.gridview);
gridview.setBackgroundResource(R.drawable.bg0);
dialog = new AlertDialog.Builder(Alertdialog.this)
.setTitle("登陆提示") //设置标题
.setMessage("这里需要登录!") //设置内容
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LayoutInflater factory = LayoutInflater.from( Alertdialog .this);
//得到自定义对话框
final View DialogView = factory.inflate(R.layout.dialog, null);
//创建对话框
AlertDialog dlg = new AlertDialog.Builder( Alertdialog .this)
.setTitle("登录框")
.setView(DialogView)//设置自定义对话框的样式
.setPositiveButton("确定",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
Intent intent = new Intent();
intent.setClass(Alertdialog.this, BrowserIntent.class);
startActivity(intent);
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
//点击"取消"按钮之后退出程序
Alertdialog.this.finish();
}
})
.create();//创建
dlg.show();//显示
}
}).create();
dialog.show();
}
}
gridview.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
BrowserIntent.java代码如下:
package com.liu;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.Button;
import android.widget.EditText;
public class BrowserIntent extends Activity {
private EditText urlText;
private Button goButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
urlText = (EditText)findViewById(R.id.url_field);
goButton = (Button)findViewById(R.id.go_button);
goButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
openBrowser();
}
});
urlText.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_ENTER){
openBrowser();
return true;
}
return false;
}
});
}
private void openBrowser(){
Uri uri = Uri.parse(urlText.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
}
最后在AndroidManifest.xml添加如下代码就行啦:
<activity android:name=".BrowserIntent" />
<uses-permission android:name="android.permission.INTERNET" />
运行的结果如下: