Android开发从零开始之Button与事件监听

本文介绍了Android开发中Button组件的基本用法及多种事件监听方法,包括XML布局定义、代码创建及不同监听器的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Button是Android开发中常用的组件之一,其功能和用法非常广,TextView,imageView应用的功能它全有,一般来讲,它是用来监听事件的,也就是被"按"的;

一,  Button可以通过两种方式新建:

1, 在xml布局文件中进入新建:

 <Button 
        android:id="@+id/bn1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="我是按钮1"
        android:textColor="#fff"
        android:textSize="20dp"/>


之后在代码中通过ID查找到这个按钮

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

2,或者在代码中新建

LinearLayout ll = new LinearLayout(this);

Button bn1 = new Button(this);

ll.addView(bn1);

二,关于监听,Android平台下对控件的监听可以通过四种方式实现

1.使用接口继承按钮监听方法:

[javascript] view plain copy print ?
  1. package dickren123.hui.say_hello_to_world;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8. /* 这里接口继承的方法是隶属于按钮BUTTON的,因此前面导入的头文件只需有BUTTON即可*/  
  9. public class Hello_to_worldActivity extends Activity implements Button.OnClickListener{  
  10. /** Called when the activity is first created. */  
  11. private Button btn_say_hello;  
  12. private TextView hello_world;  
  13. @Override  
  14. public void onCreate(Bundle savedInstanceState) {  
  15. super.onCreate(savedInstanceState);  
  16. setContentView(R.layout.main);  
  17. btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);  
  18. hello_world = (TextView)findViewById(R.id.textView1);  
  19. btn_say_hello.setOnClickListener(this) ;//由于该类继承了BUTTON的监听,   
  20. //因此设置监听的参数只需传本类的对象即可   
  21. public void onClick(View v) {  
  22. // TODO Auto-generated method stub   
  23. hello_world.setText("dickren123!");//抽象接口的内部方法的实现   
  24. }  
  25. }  
package dickren123.hui.say_hello_to_world;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/* 这里接口继承的方法是隶属于按钮BUTTON的,因此前面导入的头文件只需有BUTTON即可*/
public class Hello_to_worldActivity extends Activity implements Button.OnClickListener{
/** Called when the activity is first created. */
private Button btn_say_hello;
private TextView hello_world;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
hello_world = (TextView)findViewById(R.id.textView1);
btn_say_hello.setOnClickListener(this) ;//由于该类继承了BUTTON的监听,
} //因此设置监听的参数只需传本类的对象即可
public void onClick(View v) {
// TODO Auto-generated method stub
hello_world.setText("dickren123!");//抽象接口的内部方法的实现
}
}



 

2.使用接口继承view类的监听方法

[javascript] view plain copy print ?
  1. package dickren123.hui.say_hello_to_world;  
  2.   
  3.   
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;/* 导入的头文件需要有view类监听*/  
  8. import android.widget.Button;  
  9. import android.widget.TextView;  
  10.   
  11.   
  12. public class Hello_to_worldActivity extends Activity implements OnClickListener{  
  13.     /** Called when the activity is first created. */  
  14. private Button btn_say_hello;  
  15. private TextView hello_world;  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);  
  21.         hello_world = (TextView)findViewById(R.id.textView1);  
  22.         btn_say_hello.setOnClickListener(this) ;//由于该类继承了view的监听,   
  23.     } //因此设置监听的参数只需传本类的对象即可   
  24. public void onClick(View v) {  
  25. // TODO Auto-generated method stub   
  26. hello_world.setText("dickren123!");//抽象接口的内部方法的实现   
  27. }  
  28. }  
package dickren123.hui.say_hello_to_world;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;/* 导入的头文件需要有view类监听*/
import android.widget.Button;
import android.widget.TextView;


public class Hello_to_worldActivity extends Activity implements OnClickListener{
    /** Called when the activity is first created. */
private Button btn_say_hello;
private TextView hello_world;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
        hello_world = (TextView)findViewById(R.id.textView1);
        btn_say_hello.setOnClickListener(this) ;//由于该类继承了view的监听,
    } //因此设置监听的参数只需传本类的对象即可
public void onClick(View v) {
// TODO Auto-generated method stub
hello_world.setText("dickren123!");//抽象接口的内部方法的实现
}
}



 

3.不用接口,在类内部直接实现监听

[javascript] view plain copy print ?
  1. package dickren123.hui.say_hello_to_world;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.TextView;  
  8.   
  9. public class Hello_to_worldActivity extends Activity {  
  10. /** Called when the activity is first created. */  
  11. private Button btn_say_hello;  
  12. private TextView hello_world;  
  13. @Override  
  14. public void onCreate(Bundle savedInstanceState) {  
  15. super.onCreate(savedInstanceState);  
  16. setContentView(R.layout.main);  
  17. btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);  
  18. hello_world = (TextView)findViewById(R.id.textView1);  
  19. btn_say_hello.setOnClickListener(new Button.OnClickListener(){  
  20. public void onClick(View v) { //使用匿名的Button实例   
  21. // TODO Auto-generated method stub   
  22. hello_world.setText("dickren123!");//抽象接口的内部方法的实现   
  23. }  
  24. }) ;  
  25. }   
  26. }  
package dickren123.hui.say_hello_to_world;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class Hello_to_worldActivity extends Activity {
/** Called when the activity is first created. */
private Button btn_say_hello;
private TextView hello_world;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);
hello_world = (TextView)findViewById(R.id.textView1);
btn_say_hello.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) { //使用匿名的Button实例
// TODO Auto-generated method stub
hello_world.setText("dickren123!");//抽象接口的内部方法的实现
}
}) ;
} 
}



 

如果不使用匿名实例,也可以定义一个具体的实例,如下:

[javascript] view plain copy print ?
  1. package dickren123.hui.say_hello_to_world;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7.   
  8. public class Hello_to_worldActivity extends Activity {  
  9. /** Called when the activity is first created. */  
  10. private Button btn_say_hello;  
  11.   
  12. @Override  
  13. public void onCreate(Bundle savedInstanceState) {  
  14. super.onCreate(savedInstanceState);  
  15. setContentView(R.layout.main);  
  16. btn_say_hello = (Button)findViewById(R.id.bnt_SAY_HELLO_TO_WORLD);  
  17. btn_listener bl = new btn_listener();  
  18. btn_say_hello.setOnClickListener(bl); //bl是类btn_listener的实例,而btn_listener为监听方法的接口   
  19. //因此设置监听的参数只需传本类的对象即可   
  20. }  
  21. class btn_listener implements Button.OnClickListener  
  22. {  
  23. public void onClick(View v) {  
  24. // TODO Auto-generated method stub   
  25.   
  26. }  
  27. }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值