事件三要素:
事件
事件源
事件监听器
一.单击事件
but.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String str = et.getText().toString();
tv.setText(str);
}
});
二. 按钮选项改变事件
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
rb = (RadioButton) findViewById(checkedId);
String txt = rb.getText().toString();
tv.setText(txt);
}
});
三.下拉列表选项事件
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapt, View view,
int position, long id) {
TextView tvv = (TextView) view;
String str = tvv.getText().toString();
tv.setText(str);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
四. 日期和时间事件
tp.setOnTimeChangedListener(new OnTimeChangedListener() {
@Override
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
tv.setText(hourOfDay+":"+minute);
}
});
dp.init(2017,0, 25, new DatePicker.OnDateChangedListener(){
@Override
public void onDateChanged(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
tv.setText(""+year+monthOfYear+dayOfMonth);
}
});
五. 焦点事件
et.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
TextView tvv = (TextView)v;
//hasFocues:是否获得焦点
if(hasFocus){
tvv.setText("");
}else{
tv.setText(tvv.getText().toString());
}
}
});