• 【安卓开发】安卓按键响应


    一、在xml中设置按键的onClick绑定的函数

    1. 在 .xml 文件中设置onClick属性,属性值为绑定的函数名:
     
    1. 在 .java 文件中添加响应函数:
     public void buttonClicked(View v){
        	switch(v.getId()){		//getId会获取响应Id号
        		case R.id.button1:	
        			Toast.makeText(this, "按键一被按下", 0).show();
        			break;
        		case R.id.button2:
        			Toast.makeText(this, "按键二被按下", 0).show();
        			break;
        	}
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    Toast.makeText(context, text, duration)方法:

    该方法主要用于按键响应调试信息的打印。

    参数设置:context为对象,text为输出内容,duration为显示时间
    
    • 1

    效果显示:

    在这里插入图片描述



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

    1. 用 findViewById() 函数绑定局部变量 btn 和xml中的button:

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

    2. 实现接口类,在自定义类中实现 onclicked 方法,写业务代码:

    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("按键一被按下");
    		//	Toast.makeText(this, "按键一被按下", 0).show();
    			break;
    		case R.id.button2:
    			System.out.println("按键二被按下");
    		//	Toast.makeText(this, "按键二被按下", 0).show();
    			break;
    		}
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

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

    		btn1.setOnClickListener(new MyClieckHandler());		//多态
            btn2.setOnClickListener(new MyClieckHandler());
    
    • 1
    • 2

    完整代码:

    .java

    package com.example.test;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    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("按键一被按下");
    		//	Toast.makeText(this, "按键一被按下", 0).show();
    			break;
    		case R.id.button2:
    			System.out.println("按键二被按下");
    		//	Toast.makeText(this, "按键二被按下", 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());
        }
        
    }         
    
    • 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

    .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="按键一" />
    
        <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="16dp"
            android:text="按键二" />
    
    RelativeLayout>
    
    • 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



    三、匿名内部类实现按键监听事件的接口(和自定义类似)

    这种方法不再创建类去继承监听事件接口 View.OnClickListener 并且实例化其对象,而是直接在 setOnClickListener 那里创建继承接口 View.OnClickListener 的匿名内部类从而实现业务的绑定。

    package com.example.test;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    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);
            
            //在这里直接继承接口View.OnClickListener实现匿名内部类
            btn1.setOnClickListener(new View.OnClickListener() {	
    			
    			@Override
    			public void onClick(View arg0) {	//通过实现接口方法实现业务代码
    				// TODO Auto-generated method stub
    				System.out.println("按键一被按下");
    				//因为在MainActivity中,所以可以实现 makeText()
    				Toast.makeText(MainActivity.this, "按键一被按下", 0).show();		
    			}
    		});
            
            //在这里直接继承接口View.OnClickListener实现匿名内部类
            btn2.setOnClickListener(new View.OnClickListener(){
    			
    			@Override
    			public void onClick(View arg0) {	//通过实现接口方法实现业务代码
    				// TODO Auto-generated method stub
    				System.out.println("按键二被按下");
    				//因为在MainActivity中,所以可以实现 makeText()
    				Toast.makeText(MainActivity.this, "按键二被按下", 0).show();
    			}
    		});
          
        }
    }             
    
    • 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

    .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="按键一" />
    
        <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="16dp"
            android:text="按键二" />
    
    RelativeLayout>
    
    • 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

    效果展示:

    在这里插入图片描述



    四、Activity实现click接口

    这种方式直接将 MainActivity 继承 View.OnClickListener 接口,并且在 setOnClickListener 的时候直接引用本类对象 this 绑定其 onClick 方法,并且在 MainActivity 类中将 onClick 进行重写实现自己想要实现的业务。

    package com.example.test;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements View.OnClickListener{
    
    	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(this);
            btn2.setOnClickListener(this);
            
        }
    
    	@Override
    	public void onClick(View v) {
    		// TODO Auto-generated method stub
    		switch(v.getId()){
    		case R.id.button1:
    			System.out.println("按键一被按下");
    			Toast.makeText(this, "按键一被按下", 0).show();
    			break;
    		case R.id.button2:
    			System.out.println("按键二被按下");
    			Toast.makeText(this, "按键二被按下", 0).show();
    			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

    .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="按键一" />
    
        <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="16dp"
            android:text="按键二" />
    
    RelativeLayout>
    
    • 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

    效果展示:

    在这里插入图片描述

  • 相关阅读:
    MySQl_2
    ros学习笔记(二)Vscode中使用Romote远程开发调试Ros2环境
    git克隆一直报错remote: HTTP Basic: Access denied
    windows下Python将程序打包成exe文件
    vs2019 无法打开QT的UI文件
    【月度总结】数据库&Python&Excel_202206
    算法基础 1.4 高精度 (加减乘除)
    Opencv实现图像的基本变换
    2022-06-15 工作记录--Vue-父页面向子组件传递数据
    详解MySQL隔离级别
  • 原文地址:https://blog.csdn.net/qq_62361050/article/details/126103179