实现一个view的监听的四种方法

方法一,用implements方法实现

 下面这段代码是实现Button的监听:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
Javapackage com.listen;
import android.app.Activity;
import android.os.Bundle;
import android.util.Lg;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class EventListen1 extends Activity implements OnClickListener { // step2实现监听
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
// step1,获取Button的id,设置监听
// 获取button01的id,并且设置监听
Button button01 = (Button)findViewById(R.id.button01);
button01.setOnClickListener(
this );
}
// step3,添加onClick方法,
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.v( " click.. " , " textview " );
}}

xml代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent" >
< Button
android:id ="@+id/button01"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="点1实现监听"
/>
</ LinearLayout >

运行结果:点击Button01,在logcat里面打印click...textview

方法二,用在xml中设置的方法实现

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
package com.listen;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class EventListen1 extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void onClickButton(View v) {
// TODO Auto-generated method stub
Log.v( " click.. " , " textview " );

}
}

xml代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
<? xml version = " 1.0 " encoding = " utf-8 " ?>
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation
= " vertical "
android:layout_width
= " fill_parent "
android:layout_height
= " fill_parent "
>
< TextView
android:id
= " @+id/textview "
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content "
android:onClick
= " onClickButton "
android:clickable
= " true "
android:text
= " @string/hello "
/>
< Button
android:id
= " @+id/button01 "
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content "
android:onClick
= " onClickButton "
android:text
= " 点1实现监听 "
/>
</ LinearLayout >

实现效果同上。

方法三:

在onCreate内部实现监听
ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
package com.listen;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class EventListen1 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1
= (Button) findViewById(R.id.button1);
/* 监听button1的事件信息 */
button1.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
Log.v(
" you click " , " button " );
}
});

}

}

xml代码如下:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>

< Button
android:id ="@+id/button1"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="点我" />

</ LinearLayout >

方法四、在class内部,onCreate外部实现监听,

注意:插入的listener是:import android.view.View.OnClickListener


ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
 

import android.app.Activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.view.View.OnClickListener;

public class ActHelloAndroid extends Activity {
/** Called when the activity is first created. */
private EditText inname;
private TextView outname;
private Button button;
private String TAG = " ActHelloAndroid " ;

@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
button
= (Button) findViewById(R.id.button);
inname
= (EditText) findViewById(R.id.edittext);
outname
= (TextView) findViewById(R.id.textview);

button.setOnClickListener(listener);
inname.setOnClickListener(listener);
outname.setOnClickListener(listener);

}

private OnClickListener listener = new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == (R.id.textview)) {
outname.setText(inname.getText()
+ " ! " + " Welcome to Android " );
}
else if (v.getId() == R.id.button) {
outname.setText(
" 你按了button! " );
}
else {
Log.v(TAG,
" haha " );
}
}

};
}
xml代码:

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
<? xml version="1.0" encoding="utf-8" ?>
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation
="vertical"
android:layout_width
="fill_parent"
android:layout_height
="fill_parent"
>
< TextView
android:layout_width ="fill_parent"
android:layout_height
="wrap_content"
android:text
="@string/hello"
/>
< EditText
android:id ="@+id/edittext"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
=""
/>

< Button
android:id ="@+id/button"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:text
="Yes"
/>
< TextView
android:id ="@+id/textview"
android:layout_width
="fill_parent"
android:layout_height
="wrap_content"
android:clickable
="true"
android:text
="dd"
/>
</ LinearLayout >
注意:当有多个view,需要点击button做出反应的时候,一定要每个view都绑定监听!!!!

效果图如图2010051513462542.png

转载于:https://www.cnblogs.com/snowdrop/articles/1734301.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值