main.xml代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="@+id/btn"
android:layout_height="wrap_content"
android:layout_width="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:id="@+id/MyLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:text="用户名:"
android:layout_marginLeft="20dip"
android:textSize="8px"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:width="60pt"
android:layout_width="wrap_content"/>
</TableRow>
<TableRow >
<TextView
android:text="密码:"
android:layout_marginLeft="20dip"
android:textSize="8px"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<EditText
android:password="true"
android:width="60pt"
android:layout_width="wrap_content"/>
</TableRow>
</TableLayout>
.java代码如下:
package org.lxh.demo;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class Hello extends Activity {
private Button btn = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 设置要使用的布局管理器
this.btn = (Button) super.findViewById(R.id.btn);
this.btn.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
LayoutInflater factory = LayoutInflater.from(Hello.this);
View myView = factory.inflate(R.layout.login, null);
Dialog dialog = new AlertDialog.Builder(Hello.this)
.setTitle("用户登录")
.setView(myView)
.setPositiveButton("登录",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
}
}).create();
dialog.show();
}
}
}