首页 > 代码库 > 安卓事件大全

安卓事件大全

1 OnClickListener(单击事件)
组件.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    String str = et.getText().toString();
    tv.setText(str);
  }
});

2 OnCheckedChangeListener(单选发生变化)
rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  public void onCheckedChanged(
    RadioGroup group, int checkedId) {
    RadioButton rb = (RadioButton)
    findViewById(checkedId);
    String str = rb.getText().toString();
    tv.setText(str);
  }
});


3 OnItemSelectedListener(下拉列表)
sp.setOnItemSelectedListener(new OnItemSelectedListener() {
  public void onItemSelected(AdapterView<?> arg0, View view,
  int arg2, long arg3) {
  TextView tv1 = (TextView) view;
  String str = tv1.getText().toString();
  tv.setText(str);
  }

public void onNothingSelected(AdapterView<?> arg0) {
}
}); 

4 OnTimeChangedListener 时间事件监听

5 OnFocusChangeListener 焦点事件
et.setOnFocusChangeListener(new OnFocusChangeListener() {
  public void onFocusChange(View v, boolean hasFocus) {
    if(hasFocus){
    et.setText("");
    }else{
    String str= et.getText().toString();
    tv.setText(str);
  }
}
});


6 OnDateChangeListener
dp.init(2017, 4, 23, new OnDateChangedListener() {
public void onDateChanged(DatePicker view, int year, int monthOfYear,
  int dayOfMonth) {
    tv.setText(year+"-"+monthOfYear+"-"+dayOfMonth);
  }
});

7 OnLongChangeListener(长按事件)
  <!-- 设置修改桌面背景权限 -->
<uses-permission android:name="android.permission.SET_WALLPAPER" />
iv.setOnLongClickListener(new OnLongClickListener() {
  public boolean onLongClick(View v) {
    try {
      Day07.this.clearWallpaper();
      //getResources();//获得资源对象
    InputStream data = http://www.mamicode.com/getResources().openRawResource(R.drawable.a);
    Day07.this.setWallpaper(data);
    } catch (IOException e) {
      e.printStackTrace();
    }
     return false;
  }
});

安卓事件大全