首页 > 代码库 > Android事件
Android事件
单击事件
button.setOnClickListener(new onClickListener(){
//匿名内部类
public void onclick(View v){
String xx= edit.getText().toString();
textview.setText(xx);
}
});
单选事件
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 选中单选按钮事件
RadioButton rb=(RadioButton)findViewById(checkedId);
String temp=rb.getText().toString();
tv1.setText("性别是:"+temp);
}
});
下拉框事件
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
//一级联
public void onItemSelected(AdapterView<?> arg0, View view,
int position, long id) {
TextView tView = (TextView) view;
string = tView.getText().toString();
tv.setText(string);
if (string.equals("湖南")) {
hnAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(hnAdapter);
} else if (string.equals("湖北")) {
hbAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(hbAdapter);
} else if (string.equals("广东")) {
gdAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(gdAdapter);
}
}
日期时间事件
tp.setOnTimeChangedListener(new OnTimeChangedListener(){
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
tView.setText(hourOfDay+":"+minute);
}});
dp.init(2017 , 06 , 19, new DatePicker.OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
tView.setText(" "+year+" "+(monthOfYear+1)+" "+dayOfMonth);
}
});
长按事件
//长按图片设置为桌面墙纸
private class PicOnLongClick implements OnLongClickListener{
@Override
public boolean onLongClick(View view){
try{
//清空当前墙纸
MainActivity.this.clearWallpaper();
//当前view转换为ImageView对象
ImageView iv=(ImageView)view;
//启用图形缓冲
iv.setDrawingCacheEnabled(true);
//使用当前缓冲图形创建Bitmap
Bitmap bmp=Bitmap.createBitmap(iv.getDrawingCache());
//当前图形设置为墙纸
MainActivity.this.setWallpaper(bmp);
//清理图形缓冲
iv.setDrawingCacheEnabled(false);
Toast.makeText(getApplicationContext(), "背景设置成功!",Toast.LENGTH_LONG).show();
}
catch(Exception e){
Toast.makeText(getApplicationContext(), "背景设置失败!",Toast.LENGTH_LONG).show();
}
return true;
}
}
键盘事件
触摸事件
Android事件