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
- 2.事件触发实验
- 相对定位父子元素触发mouseover和mouseout事件实验
- 触发事件
- JS 触发事件 JavaScript触发
- datagrid触发javascript事件
- onActivate事件何时触发?
- javascript控制事件触发
- ASP定时触发事件
- 用户控件触发事件
- 用户控件触发事件
- .NET触发MouseWheel事件
- 按回车触发事件
- js触发事件收集
- C#中的事件触发
- 用户控件触发事件
- 关闭网页触发事件
- 动态触发事件
- C#触发事件
- Android之退出系统
- 7. Reverse Integer
- Run SS as Daemon
- json转java实体
- 《从零开始学Swift》学习笔记(Day 38)——构造函数与存储属性初始化
- 2.事件触发实验
- Scala学习笔记(五) ---- try-catch-finally表达式,for表达式
- bumblebee optirun glxspheres64:X Error of failed request: GLXBadContext错误解决
- arraylist size()==0,null,isEmpty()的区别
- 【Android动画】之Tween动画 (渐变、缩放、位移、旋转)
- 哈夫曼树问题
- eclipse 安装jetty插件
- C语言 打渔晒网问题 017
- realloc函数使用总结