标签:button 设置 com col ons 匿名 oncreate change main
(6) DatePicker 事件监听
onDateChangedListener: 日期改变时触发
实现事件监听器的方法:
将事件监听器类定义在当前类的内部
public class Test extends Activity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.about);
MyButtonlistener listener = new MyButtonlistener();
button.setOnClickListener(listener);
}
class MyButtonlistener implements View.OnClickListener{
@Override
public void onClick(View v) {
//相关事件处理
}
}
}
Test类
public class Test extends Activity{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.about);
MyButtonlistener listener = new MyButtonlistener();
button.setOnClickListener(listener);
}
}
MyButtonlistener类
public class MyButtonlistener implements View.OnClickListener {
@Override
public void onClick(View v) {
//事件处理
}
}
将Activity本身作为事件监听器类
public class Test extends Activity implements View.OnClickListener{
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.about);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//事件处理
}
}
public class Test extends Activity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.about);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//事件处理
}
});
}
}
在xml的布局文件中加入onClick属性,设置相关的方法
定义一个方法,记得参数是View
标签:button 设置 com col ons 匿名 oncreate change main
原文地址:http://www.cnblogs.com/kexing/p/7616285.html