其他分享
首页 > 其他分享> > 按键响应之接口类(OnClickListener)

按键响应之接口类(OnClickListener)

作者:互联网

自定义类实现按键监听事件的接口

(1)绑定局部变量button和xml中的button,findViewById

  (2)   实现接口类,在类中实现onclicked方法,写业务代码

(3)为1,2两部做的准备进行绑定。使得按键被按下后执行你写的业务代码

acitivity_main.xml代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按键1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginTop="40dp"
        android:text="按键2" />

</RelativeLayout>

MainActivity.java代码

package com.example.bha.button;

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


class  MyClieckHandler implements View.OnClickListener{

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch(v.getId())
   	 {
   	    case  R.id.button1:
   	    	 System.out.println("按键1被按下");
   	    	// Toast.makeText(this, "按键1被按下", 0).show();
   	    	 break;
   	    
   	    case  R.id.button2:
  	    	     System.out.println("按键2被按下");
  	    	   //  Toast.makeText(this, "按键2被按下", 0).show();
  	    	     break;
   	 }
		
	}
	 
	
}




public class MainActivity extends Activity {

	Button btn1;
	Button btn2;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        
        btn1=(Button) findViewById(R.id.button1);
        btn2=(Button) findViewById(R.id.button2);
        
        
        btn1.setOnClickListener(new MyClieckHandler());
        btn2.setOnClickListener(new MyClieckHandler());
        
    }

  
    
}

代码分析:

布局变量

Button btn1;
Button btn2;

绑定xml

btn1=(Button) findViewById(R.id.button1);
btn2=(Button) findViewById(R.id.button2);

设置监听器

btn1.setOnClickListener(new MyClieckHandler());
btn2.setOnClickListener(new MyClieckHandler());

实现监听器之前要实现类,先要实现OnClickListener接口

class  MyClieckHandler implements View.OnClickListener{

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		switch(v.getId())
   	 {
   	    case  R.id.button1:
   	    	 System.out.println("按键1被按下");
   	    	// Toast.makeText(this, "按键1被按下", 0).show();
   	    	 break;
   	    
   	    case  R.id.button2:
  	    	     System.out.println("按键2被按下");
  	    	   //  Toast.makeText(this, "按键2被按下", 0).show();
  	    	     break;
   	 }
		
	}
	 
	
}

运行效果:

 

注:implements表示接口的概念

——@上官可编程

标签:MyClieckHandler,OnClickListener,Button,btn1,按键,接口类,id,btn2
来源: https://blog.csdn.net/weixin_42900834/article/details/120623962