3_5消息对话框应用示例
源码如下:
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/user"
android:text="用户名"
android:textSize="18sp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/userEdit"
android:textSize="18sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="18sp"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/paswdEdit"
android:textSize="18sp"/>
</LinearLayout>
MainActivtiy.java
package com.example.ex3_5;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
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.button1);
btn2=(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new mClick());
btn2.setOnClickListener(new mClick());
}
class mClick implements View.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("确定",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.paswdEdit);
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();
}
}
}
运行结果