第二十六篇-单击事件、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组成的,我们现在学的控制语句 ...
随机推荐
- Percona-xtrabackup 使用详解与原理
现在有个需求需要对使用 innodb 的数据库进行热备.网上查了很多工具皆推荐 Percona-xtrabackup 于是就仔细了解调研一番. 我们可以前往 https://www.percona.c ...
- 在python中定义二维数组
发表于 http://liamchzh.0fees.net/?p=234&i=1 一次偶然的机会,发现python中list非常有意思. 先看一段代码 [py]array = [0, 0, 0 ...
- Python实现百度贴吧自动顶贴机
开发这款小工具,我们需要做一些准备: url.txt:多个需要顶起的帖子地址. reply:多条随机回复的内容. selenium:浏览器自动化测试框架 首先,我们先使用pip完成selenium的安 ...
- 转载 大话pcie
原文https://blog.csdn.net/abcamus/article/details/76167747 一.PCIe DMA机制 PCIe控制器也提供DMA(Direct Memory ac ...
- Running Web API using Docker and Kubernetes
Context As companies are continuously seeking ways to become more Agile and embracing DevOps culture ...
- Ontology
本体网络(Ontology) 新一代分布式信任链网 在开始了解项目之前,让我们先看一段“第一财经”频道关于“本体网络”的介绍: 项目介绍 1摘要 类型 提供不同分布式应用场景的开放基础模块,构建跨链 ...
- git在Linux下的安装
参考:https://git-scm.com/book/zh/v1/%E8%B5%B7%E6%AD%A5-%E5%AE%89%E8%A3%85-Git Git 的工作需要调用 curl,zlib,op ...
- kubernetes Helm基本操作
创建: helm install --name demo --set Persistence.Enabled=false stable/jenkins 查看: kubectl get po,svc - ...
- U盘快速启动热键
各个品牌电脑U盘快速启动热键如下:
- C# 动态调用泛型方法
static void Main(string[] args) { #region 具体类型可传递. Personal specifiedPersonal = new Personal(); Empl ...