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); } });
长按事件
//长按图片设置为桌面墙纸 iView.setOnLongClickListener(new OnLongClickListener() { public boolean onLongClick(View v) { try { EXbg.this.clearWallpaper(); EXbg.this.setWallpaper( EXbg.this.getResources().openRawResource(R.drawable.a)); tView.setText("设置成功!"); } catch (Exception e) { tView.setText("设置失败!"); e.printStackTrace(); } return true; //两个事件 long--click //如果为true 运行两个事件 先运行long 再运行click //如果为false 只运行long } });
键盘事件
editText.setOnKeyListener(new OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if (event.getAction()==KeyEvent.ACTION_UP) { String str=editText.getText().toString(); if (str.matches("\\w+[@]\\w+[.]\\w+")) { im.setImageResource(R.drawable.a); tv.setText("格式正确!"); }else { im.setImageResource(R.drawable.icon); tv.setText("格式错误!"); } } return false; } });
触摸事件
eText.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { eText.setText((int) (event.getX()+event.getY())); return false; } });