Android 实现监听事件的几种方法

一、通过内部类实现

public class EventActivity extends AppCompatActivity {
    private Button mBtnEvent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        mBtnEvent=findViewById(R.id.btn_event_second);
        //内部类实现
        mBtnEvent.setOnClickListener(new OnClick());
    }
    
    class OnClick implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_event_second:
                    ToastUtil.showMsg(EventActivity.this,"click...");
                    break;
            }
        }
    }
}

二、通过匿名内部类实现

public class EventActivity extends AppCompatActivity {
    private Button mBtnEvent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        mBtnEvent=findViewById(R.id.btn_event_second);
        //匿名内部类
        mBtnEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.showMsg(EventActivity.this,"click...");
            }
        });

    }
}

三、通过事件源所在类实现

public class EventActivity extends AppCompatActivity implements View.OnClickListener{
    private Button mBtnEvent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        mBtnEvent=findViewById(R.id.btn_event_second);
        mBtnEvent.setOnClickListener(EventActivity.this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_event_second:
                ToastUtil.showMsg(EventActivity.this,"click...");
                break;
        }
    }   
}         

四、通过外部类实现

  1. 新建一个外部类:
public class MyClickListener implements View.OnClickListener {
    private Activity mActivity;
    public MyClickListener(Activity activity){
        this.mActivity=activity;
    }

    @Override
    public void onClick(View v) {
        ToastUtil.showMsg(mActivity, "click.....");
    }
}
  1. 调用外部类构造方法:
public class EventActivity extends AppCompatActivity {
    private Button mBtnEvent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
        
        mBtnEvent.setOnClickListener(new MyClickListener(EventActivity.this));
    }  
}          

五、布局文件中 onClick 属性实现

  1. 布局文件添加 onClick :
    <Button
        android:id="@+id/btn_event_second"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="click me"
        android:textAllCaps="false"
        android:onClick="show"/>
  1. Activity 文件添加 onClick 中调用的方法:
public class EventActivity extends AppCompatActivity {
    private Button mBtnEvent;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_event);
    }
    public void show(View view){
        switch (view.getId()){
            case R.id.btn_event_second:
                ToastUtil.showMsg(EventActivity.this,"click from show");
                break;
        }
    }
}    

Android 实现监听事件的几种方法_第1张图片

♥ 喜 欢 请 点 赞 哟 ♥
(●ˇ∀ˇ●)

你可能感兴趣的:(Android,学习笔记)