今天笔者来说下Android中OnClick事件监听的几种书写方式。
- 匿名内部类的方式
package com.example.testofonclick;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
//1、声明控件
private Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//2、绑定事件
btnLogin = (Button) findViewById(R.id.btn_cancel);
//3、设置触发事件
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
Toast.makeText(MainActivity.this, "已经点击。", Toast.LENGTH_SHORT).show();
}
});
}
}
- 让MainActivity 实现(Implements)View.OnClickListener方法:
package com.example.testofonclick;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//1、声明控件
private Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
//2、绑定事件
btnCancel= (Button) findViewById(R.id.btn_cancel);
//3、设置触发事件
btnCancel.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_cancel:
Toast.makeText(MainActivity.this, "已经点击。", Toast.LENGTH_SHORT).show();
finish();
break;
}
}
}
- 在布局文件中给定一个OnClik(代码如下):
<Button
android:onClick="btnCancel"
android:id="@+id/btn_login"
android:text="点击事件(点击退出)"
android:layout_width="368dp"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="0dp"
tools:layout_editor_absoluteX="8dp" />
之后再直接调用:
package com.example.testofonclick;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnCancel(View view){
finish();
}
}
- 代码实现如下:
package com.example.testofonclick;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin = (Button) findViewById(R.id.btn_login);
btnLogin.setOnClickListener(new MyClick());
}
private class MyClick implements View.OnClickListener{
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn_login:
Toast.makeText(MainActivity.this, "已经点击。", Toast.LENGTH_SHORT).show();
finish();
break;
}
}
}
}
- 普通方法如下:
package com.example.testofonclick;
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 {
private Button btnCancel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnCancel= (Button) findViewById(R.id.btn_login);
btnCancel.setOnClickListener(click);
}
//创建监听对象
Button.OnClickListener click = new Button.OnClickListener(){
@Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "已经点击。", Toast.LENGTH_SHORT).show();
finish();
}
};
}