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;

        }
    }

}

 

 

 

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 @ 2020-05-18 19:14  CrushGirl  阅读(616)  评论(0编辑  收藏  举报