方法一,用implements方法实现
下面这段代码是实现Button的监听:
代码
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 " );
}}
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代码如下:
代码
<?
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 >
< 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中设置的方法实现
代码
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 " );
}
}
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代码如下:
代码
<?
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 >
< 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内部实现监听
代码
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 " );
}
});
}
}
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代码如下:
代码
<?
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 >
< 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
代码
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代码:
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 " );
}
}
};
}
代码
注意:当有多个view,需要点击button做出反应的时候,一定要每个view都绑定监听!!!!
<?
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 >
< 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 >
效果图如图