单击事件有3种方法:

第一种:

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <TextView
android:textColor="@color/text_color"
android:textSize="@dimen/text_size"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_name" /> <EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <TextView
android:textColor="@color/text_color"
android:textSize="@dimen/text_size"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" /> <EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <Button
android:textColor="@color/button_color"
android:textSize="@dimen/button_size"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login" /> <Button
android:textColor="@color/button_color"
android:textSize="@dimen/button_size"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancle" />
</LinearLayout>
</LinearLayout>

MainActivity.java

package com.example.aimee.resoucetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity {
Button button1,button2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
button1=findViewById(R.id.button);
button2=findViewById(R.id.button2); // button1.setOnClickListener(this);
// button2.setOnClickListener(this); button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();
}
}); button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"退出",Toast.LENGTH_SHORT).show();
}
});
}
}

第二种:添加单击事件的入口,通过switch判断单击的事件

MainActivity.java

package com.example.aimee.resoucetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button button1,button2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
button1=findViewById(R.id.button);
button2=findViewById(R.id.button2); button1.setOnClickListener(this);
button2.setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_LONG).show();
break;
case R.id.button2:
Toast.makeText(MainActivity.this,"退出",Toast.LENGTH_LONG).show();
break;
}
}
}

第三种:

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <TextView
android:textColor="@color/text_color"
android:textSize="@dimen/text_size"
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/user_name" /> <EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <TextView
android:textColor="@color/text_color"
android:textSize="@dimen/text_size"
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/password" /> <EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"> <Button
android:onClick="login"
android:textColor="@color/button_color"
android:textSize="@dimen/button_size"
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/login" /> <Button
android:onClick="cancel"
android:textColor="@color/button_color"
android:textSize="@dimen/button_size"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cancle" />
</LinearLayout>
</LinearLayout>

MainActivity.java

package com.example.aimee.resoucetest;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
} public void login(View view) {
Toast.makeText(MainActivity.this,"第三种单击事件方法",Toast.LENGTH_LONG).show();
} public void cancel(View view) {
Toast.makeText(MainActivity.this,"退出",Toast.LENGTH_LONG).show();
}
}

Toast事件,第一个参数一般写this,如果this出错,就用类的名称.this,比如这个里面就是MainActivity.this。第二个参数写想显示的字符串,第三个参数是悬浮时间的长短,一般是Toast.LENGTH_LONG或者Toast.LENGTH_SHORT,后面的.show()是将其显示出来。

第二十六篇-单击事件、Toast(提示框信息)的更多相关文章

  1. 第二十六篇 jQuery 学习8 遍历-父亲兄弟子孙元素

    jQuery 学习8 遍历-父亲兄弟子孙元素   jQuery遍历,可以理解为“移动”,使用“移动”还获取其他的元素.   什么意思呢?老师举一个例子: 班上30位同学,我是新来负责教这个班学生的老师 ...

  2. 第二十六篇:两个SOUI新控件 ---- SListView和SComboView(借用Andorid的设计)

    SOUI原来实现的SListBoxEx的效率一直是我对SOUI不太满意的地方.包括后来网友实现的SListCtrlEx. 这类控件为每一个列表项创建一个SWindow来容纳数据,当数据量比较大(100 ...

  3. Python之路(第二十六篇) 面向对象进阶:内置方法

    一.__getattribute__ object.__getattribute__(self, name) 无条件被调用,通过实例访问属性.如果class中定义了__getattr__(),则__g ...

  4. SpringBoot非官方教程 | 第二十六篇: sprinboot整合elk,搭建实时日志平台

    转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/sprinboot25-elk/ 本文出自方志朋的博客 这篇文章主要介绍 ...

  5. Android UI开发第二十六篇——Fragment间的通信

    为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把它们跟一个Activity关联,并 ...

  6. 第二十六篇 -- wifi学习

    参考网址:https://blog.csdn.net/zwl1584671413/article/details/77936950 https://blog.csdn.net/Righthek/art ...

  7. 第二十六篇、因为自定item(nav)而使系统右滑返回手势失效的解决方法

    @interface ViewController () <uigesturerecognizerdelegate> @end@implementation ViewController ...

  8. 第二十六篇:USB3.0高带宽ISO(48KBytes/125us)实战

    USB3.1技术已经推出, 10Gbps的速率足以满足数据, HD视频传输的要求. 要步入USB3.1的研发, 还得将USB3.0的基础打扎实. 微软提供的SUPER MUTT仅仅包括一个接口0, 其 ...

  9. flask第二十六篇——模板【控制语句】【2】

    如果你也在学flask,就请加船长的公众号:自动化测试实战 我们先补充一下for循环的知识,我们之前说过,flask是由Jinja2+sqlAlchemy+werkzeug组成的,我们现在学的控制语句 ...

随机推荐

  1. Redis五大数据类型

    首先说明下,Redis是:单线程+多路IO复用技术!!! string set  >  key  +  zset          list hash 常用的几个命令: >keys * 查 ...

  2. select、poll、epoll之间的区别(搜狗面试)

    (1)select==>时间复杂度O(n) 它仅仅知道了,有I/O事件发生了,却并不知道是哪那几个流(可能有一个,多个,甚至全部),我们只能无差别轮询所有流,找出能读出数据,或者写入数据的流,对 ...

  3. python学习笔记(5)-time库的使用

    import time 一.时间获取函数 time(), ctime(),gmtime() >>> import time >>> time.time() 1524 ...

  4. Cent OS安装使用ffmpeg(完整版)

    Cent OS安装使用ffmpeg centos作为主流后台linux 系统,ffmpeg作为视频流解析的主力,尤其是ffmpeg配合opencv使用,则是视觉操作的基础 版本: ffmpeg3.1 ...

  5. MD5进行加密操作

    package com.dyy.test; import java.security.MessageDigest; import java.security.NoSuchAlgorithmExcept ...

  6. Luogu5245 【模板】多项式快速幂(多项式exp)

    A(x)k=eklnA(x).泰勒展开之后容易发现k并非在指数上,所以对p取模. #include<iostream> #include<cstdio> #include< ...

  7. P1914 一串字母

    P1914 题目背景 某蒟蒻迷上了“小书童”,有一天登陆时忘记密码了(他没绑定邮箱or手机),于是便把问题抛给了神犇你. 题目描述 蒟蒻虽然忘记密码,但他还记得密码是由一串字母组成.且密码是由一串字母 ...

  8. visual studio 显示引用关系 作者更改项

    visual studio 2017中,每个类或者方法顶部会显示此方法的引用关系或者作者更改项:这个功能极大了提高了我们代码的定位效率:不过有时候却发现每一行代码都有,会增加我们屏幕显示内容,有时候看 ...

  9. 洛谷P1897电梯里的爱情题解

    题目 这个题是一个搜索题,可以先算出最高楼层,并算出不重复的楼层的个数,要注意的一点就是一定不要把0楼算在内. 代码 #include<iostream> #include<cstr ...

  10. selenium+python启动Firefox浏览器失败问题和点击登陆按钮无效问题

    问题1:使用python+selenium编写脚本调用Firefox时报错: