首页 > 系统 > Android > 正文

Android开发之事件处理

2019-11-07 23:49:35
字体:
来源:转载
供稿:网友

很简单的一个事件处理应用,只是熟悉Android的使用,点击事件触发对话框

登录账号密码为Android;123456

对于Toast信息,

Toast.makeText(MainActivity.this, "一个Toast", Toast.LENGTH_SHORT).show();

这个方法带三个参数,context,text,duration。context是一个上下文类型,写上使用这个方法的java类名加上.this即可,text是Toast要显示的信息,duration是Toast显示的时间长度,有Toast.LENGTH_SHORT和Toast.LENGTH_LONG可选,最后记得调用show()方法将Toast显示出来。

这里的事件处理有两个,一个是Button的单击事件,button.setOnClickListener(), 还有一个是RadioGroup的切换事件,radioGroup.setOnCheckedChangeListener()

简单对话框AlertDialog,首先创建AlertDialog.Bulider对象;

标题,通过setTitle()方法设置,图标,通过setIcon()方法设置,显示在中间的主要信息,通过setMessage()方法显示,等等;

如果需要设置取消按钮,大概是这样的:

                    builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {                        @Override                        public void onClick(DialogInterface dialogInterface, int i) {                            Toast.makeText(MainActivity.this, "取消按钮被点击", Toast.LENGTH_SHORT).show();                        }                    });源码地址


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表