Android学习07-----事件处理(3)监听日期与时间的改变_焦点事件_长按事件(修改桌面)

四、监听日期与时间的改变

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/input"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent"
    	android:orientation="vertical" >
    
        <DatePicker 
            android:id="@+id/date"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"/>
        
        <TimePicker
            android:id="@+id/time"
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"/>
    
    </LinearLayout>

</LinearLayout>

Activity:

package com.iflytek.activity;

import android.app.Activity;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.EditText;
import android.widget.TimePicker;
import android.widget.TimePicker.OnTimeChangedListener;

public class EventActivity extends Activity {

	private EditText input = null;
	private DatePicker date = null;
	private TimePicker time = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);

		input = (EditText) super.findViewById(R.id.input);
		date = (DatePicker) super.findViewById(R.id.date);
		time = (TimePicker) super.findViewById(R.id.time);

		// 设置24小时制显示时间
		this.time.setIs24HourView(true);

		// 时间监听
		this.time.setOnTimeChangedListener(new OnTimeChangedListenerImpl());

		// 日期监听器,注意这里的日期监听方式和以前有所区别
		this.date.init(this.date.getYear(), this.date.getMonth(),
				this.date.getDayOfMonth(), new OnDateChangedListenerImpl());
		
		this.setDateTime();

	}

	private class OnTimeChangedListenerImpl implements OnTimeChangedListener {

		@Override
		public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
			EventActivity.this.setDateTime();
		}

	}

	private class OnDateChangedListenerImpl implements OnDateChangedListener {

		@Override
		public void onDateChanged(DatePicker view, int year, int monthOfYear,
				int dayOfMonth) {
			EventActivity.this.setDateTime();
		}
	}

	public void setDateTime() {
		//注意这里的月份是从0开始的
		this.input.setText(this.date.getYear() + "-" + (this.date.getMonth()+1)
				+ "-" + this.date.getDayOfMonth() + " "
				+ this.time.getCurrentHour() + ":"
				+ this.time.getCurrentMinute());

	}

}
 

五、焦点事件

验证输入Demo

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

	<EditText
		android:id="@+id/edit"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="请输入查询内容"/>
	<EditText
		android:id="@+id/msg"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="xdwang"/>
	<TextView 
		android:id="@+id/txt"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"/>

</LinearLayout>

Activity:

package com.iflytek.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
import android.widget.TextView;

public class EventActivity extends Activity {

	private EditText edit = null;
	private TextView txt = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);

		this.edit = (EditText) super.findViewById(R.id.edit);
		this.txt = (TextView) super.findViewById(R.id.txt);

		this.edit.setOnClickListener(new OnClickListenerImpl());
		this.edit.setOnFocusChangeListener(new OnFocusChangeListenerImpl());

	}

	private class OnClickListenerImpl implements OnClickListener {
		@Override
		public void onClick(View v) {
			EventActivity.this.edit.setText("");// 清空文本
		}
	}

	private class OnFocusChangeListenerImpl implements OnFocusChangeListener {
		@Override
		public void onFocusChange(View view, boolean hasFocus) {
			// view表示操作的组件,hasFocus表示是否获得焦点
			if (hasFocus) {// 已经获得了焦点
				EventActivity.this.txt.setText("获得了焦点");
			} else {
				if (EventActivity.this.edit.getText().length() > 0) {// 现在有数据
					EventActivity.this.txt.setText("文本输入组件失去焦点,输入内容合法");
				} else {
					EventActivity.this.txt.setText("文本输入组件失去焦点,输入内容不能为空!");
				}

			}
		}
	}

}

六、长按事件

设置桌面Demo

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

	<TextView 
		android:id="@+id/info"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:text="长按图片将设置为桌面背景" />
	<ImageView
		android:id="@+id/img"
		android:layout_width="fill_parent"
		android:layout_height="fill_parent"
		android:src="@drawable/james_java"/> 

</LinearLayout>
 

AndroidManifest.xml加上:

<!-- 允许设置桌面 -->
    <uses-permission android:name="android.permission.SET_WALLPAPER" />
 

Activity:

package com.iflytek.activity;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class EventActivity extends Activity {

	private TextView info = null;
	private ImageView img = null;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.main);

		this.img = (ImageView) super.findViewById(R.id.img);
		this.info = (TextView) super.findViewById(R.id.info);
		
		this.img.setOnLongClickListener(new OnLongClickListenerImpl());
	}

	private class OnLongClickListenerImpl implements OnLongClickListener {
		@Override
		public boolean onLongClick(View v) {
			try {
				EventActivity.this.clearWallpaper();// 清除已有的桌面
				EventActivity.this.setWallpaper(EventActivity.this.img
						.getResources().openRawResource(R.drawable.james_java));// 设置桌面
				EventActivity.this.info.setText("桌面背景已修改.");
			} catch (Exception e) {
				e.printStackTrace() ;
				EventActivity.this.info.setText("桌面背景修改失败!");
			}
			return false;
		}
	}
}

相关推荐