activity_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:gravity="center_horizontal"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开普通对话框"
android:textSize="20sp"/>
<Button
android:id="@+id/button2"
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/userEdit"
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/passwdEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp"/>
</LinearLayout>
MainActivity.java
package com.example.hw3_5;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
ProgressDialog mydialog;
Button btn1,btn2;
LinearLayout login;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1=(Button)findViewById(R.id.button1);
btn2=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new mClick());
btn2.setOnClickListener(new mClick());
}
class mClick implements OnClickListener{
Builder dialog =new AlertDialog.Builder(MainActivity.this);
@Override
public void onClick(View arg0){
if(arg0==btn1){
dialog.setTitle("警告");
dialog.setIcon(R.drawable.icon1);
dialog.setMessage("本项操作可能导致信息泄露");
dialog.setPositiveButton("确定", (DialogInterface.OnClickListener) 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{
@Override
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.passwdEdit);
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{
@Override
public void onClick(DialogInterface dialog,int which){
MainActivity.this.finish();
}
}
}
运行结果如图