AS练习——消息对话框

代码

 activity_main.xml(主界面布局)




    

login.xml(登录界面布局)




    

    

    

    

MainActivity.java(主控制文件)

package com.example.firstapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
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 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 View.OnClickListener{
        AlertDialog.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;
        public void onClick(DialogInterface dialog,int which){
            txt=(EditText)login.findViewById(R.id.paswdEdit);
            if ((txt.getText().toString()).equals("123"))
                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){
            dialog.cancel();
        }
    }

}

结果显示:

AS练习——消息对话框_第1张图片               主界面布局效果图

AS练习——消息对话框_第2张图片        普通对话框,有"警告"消息提示,点击确定可返回主界面

AS练习——消息对话框_第3张图片         用户登录对话框,输入用户名和密码进行登录。若密码匹配正确,则登录成功,消息提示“登录成功”,反之登录失败,返回主界面,消息提示“密码错误”。点击退出按钮可返回主界面。

AS练习——消息对话框_第4张图片

你可能感兴趣的:(计算机)