setView(view)方法来显示登录框。接受的参数为View(view,editText的组合),以LayoutInflater来实现。
要得到LayoutInflater(布局泵),只需要调用
LayoutInflater inflater =
(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
我们用inflater .inflater()用来找layout下xml布局文件,并且实例化。
类似于findVIewbyID,区别是一个得到整个布局,一个得到单个的组件。
代码如下:
case R.id.button5:
LayoutInflater inflater
=
(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view =
inflater.inflate(R.layout.dialog, null);
new
AlertDialog.Builder(this)
.setTitle("登陆框")
.setIcon(R.drawable.img4)
.setView(view)
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int
which) {
DialogActivity.this.showToast("正在登录,请稍后。。。");
}
}).show();
res/layout/dialog.xml布局为两个TextView,两个EditText,如下:
xmlns:Android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账号"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/username"
/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
/>
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/password"
/>