用于构建复杂的Dialog显示界面
main.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="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录" />
</LinearLayout>
构建页面的布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名:"
android:textSize="8px" />
<EditText
android:layout_height="wrap_content"
android:width="60pt" />
</TableRow>
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="8px" />
<EditText
android:layout_height="wrap_content"
android:width="60pt" />
</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.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Hello extends Activity {
private Button login = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 生命周期方法
super.setContentView(R.layout.main); // 设置要使用的布局管理器
this.login = (Button) super.findViewById(R.id.login);
this.login.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View arg0) {
LayoutInflater factory = LayoutInflater.from(Hello.this);//界面创建工厂
View myView = factory.inflate(R.layout.login, null);//将布局文件转变成View
Dialog dialog = new AlertDialog.Builder(Hello.this)
.setTitle("用户登录")
.setView(myView)//设置组件!!!!!</span></strong>
.setPositiveButton("登录",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
}
})
.setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
}
}).create();
dialog.show();
}
}
}
运行如下: