首先是布局文件 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开普通对话框"
android:textSize="20sp"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开输入对话框"
android:textSize="20sp"/>
</LinearLayout>
然后是登录对话框的布局文件login.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/user"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="18sp"/>
<EditText
android:id="@+id/ed1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
<TextView
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="18sp"/>
<EditText
android:id="@+id/ed2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
最后是控制文件 MainActivity.java
package com.example.example3_5;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ProgressDialog mydialog;
Button btn1, btn2;
LinearLayout login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new mClick());
btn2.setOnClickListener(new mClick());
}
class mClick implements View.OnClickListener {
Builder dialog = new AlertDialog.Builder(MainActivity.this);
public void onClick(View arg0) {
if (arg0 == btn1) {
dialog.setTitle("警告");
//dialog.setIcon(R.drawable.icon1);
dialog.setMessage("你的操作会导致信息泄露");
dialog.setPositiveButton("'确定", new okClick());
dialog.create();
dialog.show();
} else if (arg0 == btn2) {
login = (LinearLayout) getLayoutInflater().inflate(R.layout.login, null);
dialog.setTitle("登陆").setMessage("请输入用户名和密码").setView(login);
dialog.setPositiveButton("确定", new loginClick());
dialog.setNegativeButton("退出", new exitClick());
//dialog.setIcon(R.drawable.icon2);
dialog.create();
dialog.show();
}
}
}
class okClick implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}
class loginClick implements DialogInterface.OnClickListener {
EditText txt;
@Override
public void onClick(DialogInterface dialog, int which) {
txt = (EditText) login.findViewById(R.id.ed2);
if ((txt.getText().toString()).equals("admin")) {
Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(getApplicationContext(), "密码错误", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}
class exitClick implements DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
MainActivity.this.finish();
}
}
}
运行结果