android监听事件大全,Android监听事件

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

今日记录Android中的监听事件(OnClickListener),以一个简单demo,介绍三种实现方法。

一、匿名内部类

该方法直接明了,能够简单直观的体现控件被点击后触发的事件,所要执行的功能,以及,用户想要完成的目的。

在demo中有一个TextView,有一个Button,包裹在LinearLayout中。

布局代码1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

android:id="@+id/layouy"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Hello World!" />

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="点击" />

java代码1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

public class extends AppCompatActivity{

private TextView textView;

private Button button;

private LinearLayout linearLayout;

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

textView = (TextView) findViewById(R.id.text);

button = (Button) findViewById(R.id.button);

linearLayout = (LinearLayout) findViewById(R.id.layouy);

/*文本控件的监听事件,内部类*/

textView.setOnClickListener(new View.OnClickListener() {

public void onClick(View v){

textView.setText("welcome to Android!");//点击后改变文本控件的文本

}

});

/*按钮控件的监听事件,内部类*/

button.setOnClickListener(new View.OnClickListener() {

public void onClick(View v){

linearLayout.setBackgroundResource(R.mipmap.background);//点击后改变布局背景

}

});

}

}

该方法一有一定缺点,当界面控件过多时,就会重复写setOnClickListener()方法,显得代码复杂,代码量庞大。

二、Activity添加接口View.OnClickListener

当控件过多时,改用接口方式实现监听事件,在接口的onClick()方法中,用switch-case方法选择控件id,添加想要实现的功能代码。

布局代码不变,修改java代码。

java代码1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

/*添加监听事件接口*/

public class extends AppCompatActivity implements View.OnClickListener{

private LinearLayout linearLayout;

private TextView textView;

private Button button;

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

/*在视图中寻找布局的实例id*/

linearLayout = (LinearLayout) findViewById(R.id.layouy);

textView = (TextView) findViewById(R.id.text);

button = (Button) findViewById(R.id.button);

/*在寻找实例化控件id同时设置接口监听事件方法*/

findViewById(R.id.text).setOnClickListener(this);

findViewById(R.id.button).setOnClickListener(this);

}

public void onClick(View v){//接口方法,传入视图参数

/*用switch-case方法设置点击事件实现的功能*/

switch (v.getId()){//以视图得到控件id

case R.id.text:

textView.setText("welcome to Android!");//点击后改变文本控件的文本

break;

case R.id.button:

linearLayout.setBackgroundResource(R.mipmap.background);//点击后改变布局背景

break;

default:

break;

}

}

}

当控件太多时,该方法在初始化按钮控件,以及在视图中寻找id的代码量依然很大,存在一定缺点。

三、在XML文件中显示指定按钮的onClick属性

如果不想写大量代码去初始化控件,寻在控件id,那就在xml文件中为控件添加onClick属性。

布局代码1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

android:id="@+id/layout"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:id="@+id/text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="settext"

android:text="Hello World!" />

android:id="@+id/button"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:onClick="setbackground"

android:text="点击" />

这里为TextView添加监听事件属性android:onClick=”settext”,

为Button添加监听事件属性android:onClick=”setbackground”。

其中settext,setbackground是要实现功能代码的方法名,在java代码中定义该方法。1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.LinearLayout;

import android.widget.TextView;

public class extends AppCompatActivity{

private LinearLayout linearLayout;

private TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

/*在视图中寻找布局的实例id*/

linearLayout = (LinearLayout) findViewById(R.id.layout);

textView = (TextView) findViewById(R.id.text);

}

/*文本控件监听事件方法*/

public void settext(View view){

textView.setText("welcome to Android!");//点击后改变文本控件的文本

}

/*按钮控件监听事件方法*/

public void setbackground(View view){

linearLayout.setBackgroundResource(R.mipmap.background);//点击后改变布局背景

}

}

这里注意 ! ! !一定要写参数View view

在定义监听事件方法,实际是实现了java的回调机制

系统调用我们定义的方法时,实际上回调的是onClick(View view)方法。

在onClick官方文档中:

onClick1void onClick (View v)

Called when a view has been clicked.ParametersvView: The view that was clicked.

当一个视图被点击时

参数,被点击的视图。所以一定要写参数View view

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值