关闭

editView 输入框停留的时间

标签: editView 输入框停留的时间
226人阅读 评论(0) 收藏 举报
分类:
package com.cardvalue.sys.widget;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;

import com.cardvalue.sys.common.MyApplication;
import com.cardvalue.sys.common.VarKeyNames;
import com.cardvalue.sys.newnetwork.Utiltools;
import com.cardvlaue.sys.R;
//com.cardvalue.sys.widget.EditData
public class EditData extends EditText{
	private String mpageName;//basiclimit  页面名字
	private String minputName;//requestAmout 控件的名字
	private String minputLabel;//标签
	private int startdata;
	private int enddata;
	private int  str1;
	private int  str2;
	private Context context ;
	private Map<String,Object>  data;
	private boolean textChang=false;
	private Date d;
	private Date d1;
	public EditData(Context context, AttributeSet attrs, int defStyleAttr) {   
		super(context, attrs, defStyleAttr);

	}

	public EditData(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context=context;
		TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.TextData);
		mpageName=typedArray.getString(R.styleable.TextData_pageName);
		minputName=typedArray.getString(R.styleable.TextData_inputName);
		minputLabel=typedArray.getString(R.styleable.TextData_inputLabel);
		addTextChangedListener(new TextWatcher() {
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				str2=s.length();
				if(s.length()!=str1){
					textChang=true;
				}
			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {	

			}
		});


	}

	public EditData(Context context) {
		super(context);


	}



	/**
	 * 是否获得焦点
	 */
	@Override
	protected void onFocusChanged(boolean focused, int direction,   
			Rect previouslyFocusedRect) {
		if(focused){
			startdata=(int)(new Date().getTime())/1000;
			Date date = new Date();
			Long time = date.getTime();
			d = new Date(time);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			str1=EditData.this.getText().toString().trim().length();
		}else if(!focused){
			enddata=(int)(new Date().getTime())/1000;
			Log.e("enddata的时间:", enddata+"");
			Date date1 = new Date();
			Long time1 = date1.getTime();
			d1 = new Date(time1);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			if(textChang){
				Log.e("enddata的时间:", enddata+"textChang=false");
				data=new HashMap<String, Object>();
				data.put("pageName", mpageName);
				data.put("inputName", minputName);
				data.put("inputLabel", minputLabel);
				data.put("onFocusTime", sdf.format(d));
				data.put("onBlurTime", sdf.format(d1));
				/*if(MyApplication.getApplication().getLocalCache()!=null&&MyApplication.getApplication().getLocalCache().getMap(VarKeyNames.UserInfo)!=null){
					Map<String,Object> user = MyApplication.getApplication().getLocalCache().getMap(VarKeyNames.UserInfo);
					data.put("userMobile", user.get("mobilePhone"));
					return;
				}else{
					data.put("userMobile", "");	
				}*/
				
				Utiltools.print(data+"====");
				//Toast.makeText(getContext(), data.toString(), 10000).show();	
				textChang=false;
				//MyApplication.getApplication().addRecord(data);
			}


		}

		super.onFocusChanged(focused, direction, previouslyFocusedRect);

	}
		
	public void setMinputName(String minputName){
		this.minputName = minputName;
	}

}
values/attrs 
<pre name="code" class="java">    <!-- 悄悄计入文本框的数据 -->
    <declare-styleable name="TextData">
        <attr name="pageName" format="string"></attr>
        <attr name="inputName" format="string"></attr>
        <attr name="inputLabel" format="string"></attr>
    </declare-styleable>

再点击按钮的时候调用这个方法,里面的id是布局总的id
public void setFocus(View view) {  
		edit_layout.setFocusable(true);  
		edit_layout.setFocusableInTouchMode(true);  
		edit_layout.requestFocus();  
		edit_layout.requestFocusFromTouch();  
	}  



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:138928次
    • 积分:2708
    • 等级:
    • 排名:第13782名
    • 原创:141篇
    • 转载:44篇
    • 译文:1篇
    • 评论:5条
    文章分类