OnClick五种事件处理
(一)内部类
1,布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloworld.com.inspur.app2.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="点击显示"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_show"
/> </LinearLayout>
2,逻辑处理
package helloworld.com.inspur.demo6; import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView; import org.w3c.dom.Text; public class MainActivity extends AppCompatActivity {
private EditText et;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText)findViewById(R.id.et);
tv=(TextView)findViewById(R.id.tv);
et.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (event.getKeyCode())
{
case KeyEvent.KEYCODE_A:
tv.setText("AAAAAA");
break;
case KeyEvent.KEYCODE_B:
tv.setText("BBB");
break;
case KeyEvent.KEYCODE_C:
tv.setText("CCC");
break;
case KeyEvent.KEYCODE_D:
tv.setText("DDD");
break;
}
return false;
}
});
}
}
(二)外部类
1,布局同上
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloworld.com.inspur.app2.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="点击显示"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_show"
/> </LinearLayout>
2,逻辑处理
package helloworld.com.inspur.app2;
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.TextView;
public class MainActivity extends AppCompatActivity {
private EditText ed;
private Button but;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.et);
but=(Button)findViewById(R.id.but);
tv_show=(TextView)findViewById(R.id.tv_show);
but.setOnClickListener(new DoClick(ed,tv_show)); } }
class DoClick implements View.OnClickListener{
private EditText ed;
private TextView tv_show; public DoClick(EditText ed, TextView tv_show) {
this.ed = ed;
this.tv_show = tv_show;
} @Override
public void onClick(View v) {
String str=ed.getText().toString();
tv_show.setText(str);
}
}
3,相对比于内部类的处理方式
不可以使用MainActivity中的成员变量,需要通过带参数的构造函数的方式将两个参数对应起来。
(三)匿名内部类
1,布局同上
2,逻辑代码处理
package helloworld.com.inspur.app2;
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.TextView;
public class MainActivity extends AppCompatActivity {
private EditText ed;
private Button but;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.et);
but=(Button)findViewById(R.id.but);
tv_show=(TextView)findViewById(R.id.tv_show);
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=ed.getText().toString();
tv_show.setText(str);
}
}); } }
(四)本类实现
package helloworld.com.inspur.app2;
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.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText ed;
private Button but;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.et);
but=(Button)findViewById(R.id.but);
tv_show=(TextView)findViewById(R.id.tv_show);
but.setOnClickListener(this); } @Override
public void onClick(View v) {
String str=ed.getText().toString();
tv_show.setText(str);
}
}
(五)组建绑定事件
1,布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="helloworld.com.inspur.app2.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et"/>
<Button
android:onClick="doClick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/but"
android:text="点击显示"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_show"
/> </LinearLayout>
2,逻辑代码的实现
package helloworld.com.inspur.app2;
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.TextView;
public class MainActivity extends AppCompatActivity{
private EditText ed;
private TextView tv_show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText)findViewById(R.id.et);
tv_show=(TextView)findViewById(R.id.tv_show);
}
public void doClick(View v)
{
String str=ed.getText().toString();
tv_show.setText(str);
}
}
OnClick五种事件处理的更多相关文章
- 事件处理之二:点击事件监听器的五种写法 分类: H1_ANDROID 2013-09-11 10:32 4262人阅读 评论(1) 收藏
首选方法二! 方法一:写一个内部类,在类中实现点击事件 1.在父类中调用点击事件 bt_dail.setOnClickListener(new MyButtonListener()); 2.创建内部类 ...
- onclick事件没有反应的五种可能情况
转自:https://blog.csdn.net/qujing_1120/article/details/76853039 onclick=”alert()” 事件没有反应的几种情况.第一:<i ...
- 探究JavaScript中的五种事件处理程序
探究JavaScript中的五种事件处理程序 我们知道JavaScript与HTML之间的交互是通过事件实现的.事件最早是在IE3和Netscape Navigator 2中出现的,当时是作为分担服务 ...
- Ngui 五种点击事件实现方式及在3d场景中点透的情况
http://www.unity蛮牛.com/thread-22018-1-1.html ngui作为unity界面插件之一中,无疑是最好用,使用最多的了从自学unity到现在界面一直使用它 由于它的 ...
- Android中Button的五种监听事件
简单聊一下Android中Button的五种监听事件: 1.在布局文件中为button添加onClick属性,Activity实现其方法2.匿名内部类作为事件监听器类3.内部类作为监听器4.Activ ...
- Android_安卓为按钮控件绑定事件的五种方式
一.写在最前面 本次,来介绍一下安卓中为控件--Button绑定事件的五种方式. 二.具体的实现 第一种:直接绑定在Button控件上: 步骤1.在Button控件上设置android:onClick ...
- Android数据存储五种方式总结
本文介绍Android平台进行数据存储的五大方式,分别如下: 1 使用SharedPreferences存储数据 2 文件存储数据 3 SQLite数据库存储数据 4 使用Cont ...
- Android经常使用的五种弹出对话框
一个Android开发中经常使用对话框的小样例,共同拥有五种对话框:普通弹出对话框,单选对话框,多选对话框,输入对话框及进度条样式对话框: <LinearLayout xmlns:android ...
- android五种布局模式
Android布局是应用界面开发的重要一环,在Android中,共有五种布局方式,分别是:LinearLayout (线性布局),FrameLayout(框架布局),AbsoluteLayout(绝对 ...
随机推荐
- MySQL安装教程&Navicat安装
一.下载MySQL http://jingyan.baidu.com/article/e3c78d64412ae83c4c85f5fd.html 首先打开MySQL官网,找到Downloads标签,点 ...
- 史上最权威的 Activiti 框架学习
Activiti5 是 由 Alfresco 软件在 2010 年 5 月 17 日发布的业务流程管理( BPM) 框架,它是覆盖了业务流程管理.工作流.服务协作等领域 的一个开源的.灵活的. ...
- 【Interleaving String】cpp
题目: Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given: ...
- [超级基础]Web安全之SQL注入由浅入深(?)
前言 断断续续看Web安全到现在了,感觉对很多基础知识还是一知半解,停留在模糊的层次.所以准备系统总结一下. Sql注入我以前一直不以为然,一是现在能sql的站确实很少,二是有像sqlmap的工具可以 ...
- HLG1125 循环小数2
循环小数 II Time Limit: 1000 MS Memory Limit: 65536 K Total Submit: 155(55 users) Total Accepted: 92(51 ...
- php函数总结
1. isset($var) 变量存在且不为NULL,则返回TRUE 变量不存在或为NULL,则返回FALSE 2. empty($var) 若变量不存在或变量值为"".0.&qu ...
- 【转】手摸手,带你用vue撸后台 系列四(vueAdmin 一个极简的后台基础模板)
前言 做这个 vueAdmin-template 的主要原因是: vue-element-admin 这个项目的初衷是一个vue的管理后台集成方案,把平时用到的一些组件或者经验分享给大家,同时它也在不 ...
- 向量内积(bzoj 3243)
Description 两个d 维向量A=[a1,a2,...,ad]与B=[b1,b2,...,bd]的内积为其相对应维度的权值的乘积和,即: 现有 n 个d 维向量x1,...,xn ,小喵喵想知 ...
- 关于C++的字符串操作
自己懒得打,就贴一个dalao的网址吧.常用. http://blog.csdn.net/fenxinzi557/article/details/51457829
- NOI2001食物链
描述 动物王国中有三类动物 A,B,C,这三类动物的食物链构成了有趣的环形.A吃B,B吃C,C吃A. 现有N个动物,以1-N编号.每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种. 有人 ...