Main.xml
<LinearLayout 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"
android:orientation="vertical">
<Button android:id="@+id/mybut"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="注册"/>
</LinearLayout>
login.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<TableRow android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="16dp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="填写登录账号"
android:selectAllOnFocus="true"/>
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="16dp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true"/>
</TableRow>
<TableRow android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话"
android:textSize="16dp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入电话号码"
android:selectAllOnFocus="true"
android:phoneNumber="true"
/>
</TableRow>
<TableRow android:gravity="center_horizontal"
android:layout_marginTop="10dp">
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点 击 注 册"/>
</TableRow>
</TableLayout>
Main.java
package com.example.logindialog;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Builder b=new AlertDialog.Builder(this);
Button but=(Button) super.findViewById(R.id.mybut);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
b.setIcon(R.drawable.ic_launcher);
b.setTitle("注册账户");
//getLayoutInflater()是用来找Layout下的xml文件,对于没有载入或者需要动态载入的需要inflate
TableLayout loginForm=(TableLayout) getLayoutInflater().inflate(R.layout.login, null);
//设置对话框显示的View为loginForm
b.setView(loginForm);
//为对话框设置一个登录按钮
b.setPositiveButton("登录", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 此处可以执行点击登录按钮之后的操作
}
});
b.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 点击取消按钮的操作可以在这里处理
}
});
b.create().show();
}
});
}
}