Android开发-设置监听器的四种方法

Android四种监听方式:

  • 实现监听的接口
  • 实现匿名内部类
  • 使用外部类
  • 直接在xml中设置监听

1、使用接口方式实现监听事件:

可直接在Activity中定义事件处理方法

优点:简洁

缺点:可能造成程序结构混乱

2、实现匿名内部类实现监听:

优点:可以在当前类中复用该监听器,可自由访问外部类的所有界面组件

3、使用外部类实现监听:

优点:当某事件监听器被多个GUI界面共享,且只要是完成某种业务逻辑的实现

缺点:不利于提供程序内聚性,不能自由访问创建GUI界面类的组件,界面不够简洁

4、直接在xml文件中设置监听:

在需要监听的控件中添加:Android:conClick="xxx"

再在布局对应的Activity中定义public void xxx(View view){}


测试程序:

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button1"
        android:text="接口方式设置监听器"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button2"
        android:text="直接在xml文件中绑定"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:onClick="button2OnClick"/>

    <Button
        android:id="@+id/button3"
        android:text="使用匿名内部类设置监听器"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/button4"
        android:text="使用外部类设置监听器"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

MainActivity:

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Context context = MainActivity.this;

    private Button button1;
    private Button button2;
    private Button button3;
    private Button button4;

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

        button1 = (Button) findViewById(R.id.button1);
        button2 = (Button) findViewById(R.id.button2);
        button3 = (Button) findViewById(R.id.button3);
        button4 = (Button) findViewById(R.id.button4);

        button1.setOnClickListener(this);

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context,"使用匿名内部类实现监听", Toast.LENGTH_SHORT).show();
            }
        });

        button4.setOnClickListener(new MyOnClickListener() {
            public void onClick(View view){
                super.onClick(view);
            }
        });
    }

    public void button2OnClick(View view){
        Toast.makeText(context,"直接在xml文件中绑定", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button1:
                Toast.makeText(context,"以接口的方式设置监听器", Toast.LENGTH_SHORT).show();
                break;
        }
    }

    class MyOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View view) {
            Toast.makeText(context,"使用外部类设置监听器", Toast.LENGTH_SHORT).show();
        }
    }
}


  • 17
    点赞
  • 57
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
步数记录器可以通过以下步骤实现: 1. 添加传感器权限和计步器传感器类型声明到AndroidManifest.xml文件中。 ``` <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION" /> <uses-feature android:name="android.hardware.sensor.stepcounter" /> <uses-feature android:name="android.hardware.sensor.stepdetector" /> ``` 2. 在Activity中获取传感器服务的实例。 ``` SensorManager sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE); ``` 3. 获取计步器传感器的实例,并注册监听器。 ``` Sensor stepSensor = sensorManager.getDefaultSensor(Sensor.TYPE_STEP_COUNTER); sensorManager.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_NORMAL); ``` 4. 在监听器中,实现步数的记录和更新。 ``` @Override public void onSensorChanged(SensorEvent event) { Sensor sensor = event.sensor; if (sensor.getType() == Sensor.TYPE_STEP_COUNTER) { int stepCount = (int) event.values[0]; updateStepCount(stepCount); } } private void updateStepCount(int stepCount) { // 记录步数并更新UI mStepCount = stepCount; mStepCountTextView.setText(String.valueOf(mStepCount)); } ``` 5. 在Activity中,可以添加计时器,定期记录步数并更新UI。 ``` private Timer mTimer; private TimerTask mTimerTask; private void startRecord() { mTimer = new Timer(); mTimerTask = new TimerTask() { @Override public void run() { updateStepCount(mStepDetector.getStepCount()); } }; mTimer.schedule(mTimerTask, 0, 1000); } private void stopRecord() { if (mTimer != null) { mTimer.cancel(); mTimer = null; } if (mTimerTask != null) { mTimerTask.cancel(); mTimerTask = null; } } ``` 以上是一个简单的步数记录器的实现示例。需要注意的是,计步器传感器的精度和准确性可能会受到设备硬件和软件版本的影响,因此需要进行测试和验证。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值