Android Button 三种监听方式

复制代码
1、一个Button控件对应一个监听
        Button buttontest;
        buttontest = (Button) findViewById(R.id.button1);
        buttontest.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Log.i("TEST", "button onClick");
            }
        });


2、多个button对应一个监听2:
public class MainActivity extends Activity implements OnClickListener  /*View.OnClickListener*/{    
    Button btn1,btn2,btn3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        btn1 =(Button) findViewById(R.id.btn1);
        btn2 =(Button) findViewById(R.id.btn2);
        btn3 =(Button) findViewById(R.id.btn3);
    
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.btn1:
            btn1.setBackgroundResource(R.drawable.red);
            break;    
        case R.id.btn2:
            btn2.setBackgroundResource(R.drawable.red);
            break;
        case R.id.btn3:
            btn3.setBackgroundResource(R.drawable.red);
            break;
        }    
    }
}



3、多个button对应一个监听1:
start = (Button) findViewById(R.id.button1);
stop = (Button) findViewById(R.id.button2);
start.setOnClickListener(mylistener );
stop.setOnClickListener(mylistener );
View.OnClickListener mylistener = new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
            case R.id.button1:
                Log.d(TAG, "Start to recorder video\n");
                start_recorde();
                break;
            case R.id.button2:
                Log.d(TAG, "Stop to recorder video\n");
                stop_recorde();
                break;
            default:
                break;
            }    
        }
    };


四、xml中绑定监听
<Button 
android:id="@+id/button1" 
android:layout_height="wrap_content"          
android:layout_width="wrap_content" 
android:onClick="mybuttonlistener">
</Button>

Button btn = (Button) findViewById(R.id.button1);
public void mybuttonlistener(View target){
    //do something5     
}
复制代码

 

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1"
        android:textSize="32sp" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2"
        android:textSize="32sp" />

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn3"
        android:textSize="32sp" />

    <Button
        android:id="@+id/btn4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn4"
        android:textSize="32sp" />
    
</LinearLayout>
复制代码

 

 

复制代码
package com.gatsby.intentbundlesend;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity  implements View.OnClickListener {
    Button btn1, btn2, btn3, btn4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
    }

    public void initView() {

        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);
        btn3 = (Button) findViewById(R.id.btn3);

        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn1:

                break;
            case R.id.btn2:

                break;
            case R.id.btn3:

                break;

        }
    }

}
复制代码

 

 

 

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
44
45
46
47
48
49
50
51
52
53
54
55
56
package com.gatsby.videotest;
 
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;
  
public class MainActivity extends Activity implements OnClickListener{
  
     Button btn1,btn2,btn3;
    VideoView videoView = null;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_main);
        
        btn1 =(Button) findViewById(R.id.btn1);
        btn2 =(Button) findViewById(R.id.btn2);
           
        btn1.setOnClickListener(this);
        btn2.setOnClickListener(this);
        
    }
  
    private void hideNavigationBar() { 
        View decorView = getWindow().getDecorView(); 
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION 
                | View.SYSTEM_UI_FLAG_FULLSCREEN; 
        decorView.setSystemUiVisibility(uiOptions); 
    
       
    private void showNavigationBar() { 
        View decorView = getWindow().getDecorView(); 
        int uiOptions = View.SYSTEM_UI_FLAG_VISIBLE; 
        decorView.setSystemUiVisibility(uiOptions); 
    }
 
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
           switch (v.getId()) {
            case R.id.btn1:
                hideNavigationBar();
                break;   
            case R.id.btn2:
                showNavigationBar();
                break;
           }
    
  
}

  

posted @   CrushGirl  阅读(638)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示