Android:消息对话框应用示例

目录

1、要求

2、设计步骤

3、效果演示


1、要求

本例中设计了两种形式的对话框程序,一种是发出提示信息的普通对话框,另一种是用户登陆对话框。

2、设计步骤

activity_main.xml




    

login.xml




    

    

    

    

MainActivity.java

package com.example.admin.example3_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=findViewById(R.id.button1);
        btn2=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("确定",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();
        }
    }
}

3、效果演示

下面三张图依次是进入页面、输入对话框页面和普通对话框页面

Android:消息对话框应用示例_第1张图片Android:消息对话框应用示例_第2张图片Android:消息对话框应用示例_第3张图片

你可能感兴趣的:(Android:消息对话框应用示例)