2.事件触发实验

来源:互联网 发布:汇丰软件开发 待遇 编辑:程序博客网 时间:2024/06/10 02:07
package com.example.administrator.myapplication;import android.content.DialogInterface;import android.support.v7.app.AlertDialog;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.RadioButton;import android.widget.RadioGroup;import android.widget.Toast;public class MainActivity extends AppCompatActivity implements View.OnClickListener{    /*声明对象*/    private Button login,register;    private EditText User,Password;    private RadioGroup m_RadioGroup;    private RadioButton m_Radio1,m_Radio2,m_Radio3,m_Radio4;    /*设置监听单选框的事件函数*/    private class myOnCheckChangeListener implements  RadioGroup.OnCheckedChangeListener    {        @Override        public void onCheckedChanged(RadioGroup group,int checkedID)        {            String str="";            switch (checkedID)            {                case R.id.RadioButton1:{                    str = "学生";                    break;                }                case R.id.RadioButton2:{                    str="老师";                    break;                }                case R.id.RadioButton3:{                    str="社团";                    break;                }                case R.id.RadioButton4:{                    str="管理者";                    break;                }                default:{}            } //switch结束框            str += "身份被选中";            Toast.makeText(MainActivity.this,str,Toast.LENGTH_SHORT).show();        }//onCheckedChanged函数    }// myOnCheckChangeListener类    @Override    protected void onCreate(Bundle savedInstanceState)    {/*onCreate()函数是在activity初始化的时候调用的,通常情况下,我们需要在onCreate()中调用setContentView(int)函数填充屏幕的UI,    一般通过findViewById(int)返回xml中定义的视图或组件的ID。子类在重写onCreate()方法的时候必须调用父类的onCreate()方法,    即super.onCreate(),否则会抛出异常。*/        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        /*通过id获得各个对象,即初始化并且进行强制类型转换*/        login = (Button) findViewById(R.id.Login);        register = (Button) findViewById(R.id.Register);        User = (EditText) findViewById(R.id.User);        Password = (EditText) findViewById(R.id.Password);        m_RadioGroup = (RadioGroup) findViewById(R.id.RadioGroup1) ;        m_Radio1 = (RadioButton) findViewById(R.id.RadioButton1);        m_Radio2 = (RadioButton) findViewById(R.id.RadioButton2);        m_Radio3 = (RadioButton) findViewById(R.id.RadioButton3);        m_Radio4 = (RadioButton) findViewById(R.id.RadioButton4);        /*设置每个按钮监听事件*/        login.setOnClickListener(this);        register.setOnClickListener(this);        m_RadioGroup.setOnCheckedChangeListener(new myOnCheckChangeListener());    }    public boolean IsOK()    {//判断输入的用户名密码是否合法且正确        String UserName = User.getText().toString();        String Pass = Password.getText().toString();        if(UserName.equals(""))        {            Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();            return false;        }        else if(Pass.equals(""))        {            Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();            return false;        }        else if(UserName.equals("Android") && Pass.equals("123456"))            return true;        else return false;    }    @Override   //监听点击的每一个按钮    public void onClick(View view)    {        switch (view.getId())        {            case R.id.Login: //点击登陆            {                if (IsOK())                {                    AlertDialog.Builder builder = new AlertDialog.Builder(this);                    builder.setTitle("提示").setMessage("登陆成功");                    builder.setNegativeButton("取消", new DialogInterface.OnClickListener()                    {                            @Override                            public void onClick(DialogInterface dialogInterface, int i)                            {                                Toast.makeText(MainActivity.this, "对话框的取消按钮被点击", Toast.LENGTH_SHORT).show();                            }                    });                    builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {                            @Override                            public void onClick(DialogInterface dialogInterface, int i) {                                Toast.makeText(MainActivity.this, "对话框的确定按钮被点击", Toast.LENGTH_SHORT).show();                            }                    });//好像类似于函数指针的用法                    builder.show();                }                break;            }            case R.id.Register: //点击注册            {                String str0="";                final String str1="身份注册功能尚未开启";                if(m_Radio1.isChecked()) str0 = "学生";                else if(m_Radio2.isChecked()) str0 = "老师";                else if(m_Radio3.isChecked()) str0 = "社团";                else if(m_Radio4.isChecked()) str0 = "管理者";                Toast.makeText(this,str0+str1,Toast.LENGTH_SHORT).show();                break;            }            default: { }        }//switch结束框    }//OnClick函数}
0 0
原创粉丝点击