Android事件处理(六)
Android事件处理(6)
这里接着上节课的内容,现在我们是要把2,3节课学到的组件的事件监听方法全部练一遍,所以接下来就是
- 监听日期与时间的改变
- 焦点事件
一下是.java代码:
package com.example.timelongclick; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.DatePicker; import android.widget.DatePicker.OnDateChangedListener; import android.widget.EditText; import android.widget.TimePicker; import android.widget.TimePicker.OnTimeChangedListener; public class Timelong extends Activity { private TimePicker time=null; private DatePicker date=null; private EditText show=null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_timelong); this.show=(EditText)super.findViewById(R.id.editText1); this.date=(DatePicker)super.findViewById(R.id.datePicker1); this.time=(TimePicker)super.findViewById(R.id.timePicker1); this.time.setIs24HourView(true); this.time.setOnTimeChangedListener(new onTimeChanged()); this.date.init(this.date.getYear(), this.date.getMonth(), this.date.getDayOfMonth(), new onDateChanged()); this.setDateTime(); } private void setDateTime() { this.show.setText(this.date.getYear()+"-"+(this.date.getMonth()+1)+"-"+this.date.getDayOfMonth()+" " +this.time.getCurrentHour()+":"+this.time.getCurrentMinute()); } private class onTimeChanged implements OnTimeChangedListener{ @Override public void onTimeChanged(TimePicker arg0, int arg1, int arg2) { Timelong.this.setDateTime(); } } private class onDateChanged implements OnDateChangedListener{ @Override public void onDateChanged(DatePicker arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub Timelong.this.setDateTime(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.timelong, menu); return true; } }
这里有两个比较重要的监听器:
OnTimeChangedListe和OnDateChangedListener
以上代码是实现监听时间的改变,并显示出来
焦点实例,定义一个显示框,二个输入框,其中一个框绑定焦点事件,也就是在这个框输入时显示获得焦点,若离开框则失去焦点。
这里是主界面
public class Timelong extends Activity { private EditText edit=null; private TextView text=null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_timelong); this.edit=(EditText)super.findViewById(R.id.edit); this.text=(TextView)super.findViewById(R.id.text); this.edit.setOnFocusChangeListener(new Focus()); } public void onClick(View v){ this.edit.setText(""); } private class Focus implements OnFocusChangeListener{ @Override public void onFocusChange(View v, boolean focus) { if(v.getId()==Timelong.this.edit.getId()){ if(focus){ Timelong.this.text.setText("文本组件获得焦点"); } else if(Timelong.this.edit.getText().length()>0){ Timelong.this.text.setText("文本组件失去焦点"); } else{ Timelong.this.text.setText("文本组件失去焦点2"); } } } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.timelong, menu); return true; } }
-
长按事件 这里也是个重点,相信大家用过很多长按事件吧像长按保存图片复制粘贴等功能,这里我们要做到一个长按图片换成手机背景图的实例,做自己的换背景图神器是不是有点小期待呢?
首先我们要在配置文件xml里定义TextView和ImageView
然后设置换图片权限:
- <uses-permissionandroid:name="android.permission.SET_WALLPAPER"/>
代码:
public class Timelong extends Activity { private TextView text=null; private ImageView image=null; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_timelong); this.text=(TextView)super.findViewById(R.id.text); this.image=(ImageView)super.findViewById(R.id.image); this.image.setOnLongClickListener(new LongClick()); } private class LongClick implements OnLongClickListener{ @Override public boolean onLongClick(View arg0) { try { Timelong.this.clearWallpaper(); Timelong.this.setWallpaper(Timelong.this.image.getResources().openRawResource(R.drawable.wall)); } catch (NotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return false; } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.timelong, menu); return true; } }
显示效果如下:
返回手机界面发现图片换好啦,有兴趣可以做一个好一点的换图神器啦