第二十六篇-单击事件、Toast(提示框信息)
单击事件有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(提示框信息)的更多相关文章
- 第二十六篇 jQuery 学习8 遍历-父亲兄弟子孙元素
jQuery 学习8 遍历-父亲兄弟子孙元素 jQuery遍历,可以理解为“移动”,使用“移动”还获取其他的元素. 什么意思呢?老师举一个例子: 班上30位同学,我是新来负责教这个班学生的老师 ...
- 第二十六篇:两个SOUI新控件 ---- SListView和SComboView(借用Andorid的设计)
SOUI原来实现的SListBoxEx的效率一直是我对SOUI不太满意的地方.包括后来网友实现的SListCtrlEx. 这类控件为每一个列表项创建一个SWindow来容纳数据,当数据量比较大(100 ...
- Python之路(第二十六篇) 面向对象进阶:内置方法
一.__getattribute__ object.__getattribute__(self, name) 无条件被调用,通过实例访问属性.如果class中定义了__getattr__(),则__g ...
- SpringBoot非官方教程 | 第二十六篇: sprinboot整合elk,搭建实时日志平台
转载请标明出处: 原文首发于https://www.fangzhipeng.com/springboot/2017/07/11/sprinboot25-elk/ 本文出自方志朋的博客 这篇文章主要介绍 ...
- Android UI开发第二十六篇——Fragment间的通信
为了重用Fragment的UI组件,创建的每个Fragment都应该是自包含的.有它自己的布局和行为的模块化组件.一旦你定义了这些可重用的Fragment,你就可以把它们跟一个Activity关联,并 ...
- 第二十六篇 -- wifi学习
参考网址:https://blog.csdn.net/zwl1584671413/article/details/77936950 https://blog.csdn.net/Righthek/art ...
- 第二十六篇、因为自定item(nav)而使系统右滑返回手势失效的解决方法
@interface ViewController () <uigesturerecognizerdelegate> @end@implementation ViewController ...
- 第二十六篇:USB3.0高带宽ISO(48KBytes/125us)实战
USB3.1技术已经推出, 10Gbps的速率足以满足数据, HD视频传输的要求. 要步入USB3.1的研发, 还得将USB3.0的基础打扎实. 微软提供的SUPER MUTT仅仅包括一个接口0, 其 ...
- flask第二十六篇——模板【控制语句】【2】
如果你也在学flask,就请加船长的公众号:自动化测试实战 我们先补充一下for循环的知识,我们之前说过,flask是由Jinja2+sqlAlchemy+werkzeug组成的,我们现在学的控制语句 ...
随机推荐
- java中解决小数精度问题
public class TestDouble { public static void main(String[] args) { Double d1 = 0.1; Double d2 = 0.2; ...
- Django模板渲染
一 . 语法 # 关于模板渲染只需要记住两种语法就可以: 1.{{ }} # 里面写变量 2.{% %} # 里面写与逻辑相关的,比如for循环 二 . 变量名 在django的模板语言中按照语法: ...
- DAY02、正式介绍python
一.编程语言介绍(***) 1.1.机器语言:直接用计算机能理解的二进制指令编写程序,直接控制硬件 1.2.汇编语言:用英文标签取代二进制指令编写程序,本质也是直接控制硬件 1.3.高级语言:用人类能 ...
- build/temp.linux-x86_64-2.7/_openssl.c:493:30: fatal error: openssl/opensslv.h: No such file or directory
解决:apt-get install libssl-dev apt install python-dev(这个可能和那个错误关系不大)
- vscode git设置远程仓库码云
https://www.cnblogs.com/klsw/p/9080041.html
- css3实现背景渐变
#grad { background: -webkit-linear-gradient(left,rgba(255,0,0,0),rgba(255,0,0,1)); /* Safari 5.1 - 6 ...
- SQL Server使用笔记
1.连接字符串 SQL Server 身份验证,如:"server=yqzhu-peter;database=WindWMNew1_DB;uid=sa;pwd=ABcd1234;Connec ...
- .net core 2.0 配置Session
本文章为原创文章,转载请注明出处 配置Session 在Startup.cs文件的ConfigureServices方法中添加session services.AddSession(); 在Start ...
- BZOJ2480Spoj3105 Mod&BZOJ1467Pku3243 clever Y——EXBSGS
题目描述 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. 输入 每个测试文件中最多包含100组测试数据. 每组数据中,每行包含3个正整数a,p,b. 当a=p ...
- ContOS7切换国内源
ContOS更换国内下载源 一,什么是yum源? yum,是Yellow dog Updater, Modified 的简称,是杜克大学为了提高RPM 软件包安装性而开发的一种软件包管理器.起初是由y ...