package com.zhn.tongxunlu;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddpersonActivity extends Activity {

	EditText editName,editPhone,editEmail;
	Button btnAdd,btnCancle;;
	Contact c;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.add_person);
		AddpersonActivity.this.setFinishOnTouchOutside(true);
		editName = (EditText) findViewById(R.id.editName);
		editPhone = (EditText) findViewById(R.id.editPhone);
		editEmail = (EditText) findViewById(R.id.editEmail);
		editName.setOnFocusChangeListener(onFocusAutoClearHintListener);
		editPhone.setOnFocusChangeListener(onFocusAutoClearHintListener);
		editEmail.setOnFocusChangeListener(onFocusAutoClearHintListener);
		btnAdd = (Button) findViewById(R.id.btnAdd);
		btnCancle = (Button) findViewById(R.id.btnCancle);
		

		btnAdd.setOnClickListener(new OnClickListener() {
			@SuppressLint("ShowToast")
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				if (TextUtils.isEmpty(editName.getText().toString())
						||TextUtils.isEmpty(editPhone.getText().toString())) {
					Toast.makeText(AddpersonActivity.this, "联系人姓名或手机号不能为空", 1)
							.show();
				} else {
					c = new Contact();
					c.setName(editName.getText().toString());
					c.setPhone(editPhone.getText().toString());

					Intent intent = getIntent();
					Bundle data = new Bundle();
					data.putSerializable("contact", c);
					intent.putExtras(data);
					AddpersonActivity.this.setResult(0, intent);
					AddpersonActivity.this.finish();
				}
			}
		});
		
		btnCancle.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				AddpersonActivity.this.finish();
			}
		});
	}

	public static OnFocusChangeListener onFocusAutoClearHintListener = new OnFocusChangeListener() {
		@Override
		public void onFocusChange(View v, boolean hasFocus) {
			EditText textView = (EditText) v;
			String hint;
			if (hasFocus) {
				hint = textView.getHint().toString();
				textView.setTag(hint);
				textView.setHint("");
			} else {
				hint = textView.getTag().toString();
				textView.setHint(hint);
			}
		}
	};
}
