package simon.contact;
import simon.contact.entity.User;
import simon.contact.entity.UserEditText;
import simon.contact.util.Constant;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ViewSwitcher.ViewFactory;
public class EditTextActivity extends Activity {
protected UserEditText userEditText = new UserEditText();
protected Button saveButton;
protected Button returnButton;
protected ImageButton imageButton;
protected AlertDialog alertDialog; //选择头像对话框
protected ImageSwitcher imageSwitcher;
protected int imgSelected; //用户选择了哪个头像
protected void initWidget() {
this.userEditText.firstName = (EditText) this.findViewById(R.id.edit_first_name);
this.userEditText.lastName = (EditText) this.findViewById(R.id.edit_last_name);
this.userEditText.company = (EditText) this.findViewById(R.id.edit_company);
this.userEditText.phone = (EditText) this.findViewById(R.id.edit_phone);
this.userEditText.officePhone = (EditText) this.findViewById(R.id.edit_office_phone);
this.userEditText.familyPhone = (EditText) this.findViewById(R.id.edit_family_phone);
this.userEditText.post = (EditText) this.findViewById(R.id.edit_post);
this.userEditText.address = (EditText) this.findViewById(R.id.edit_address);
this.userEditText.email = (EditText) this.findViewById(R.id.edit_mail);
this.userEditText.remark = (EditText) this.findViewById(R.id.edit_remark);
this.imageButton = (ImageButton) super.findViewById(R.id.btn_img);
this.saveButton = (Button) this.findViewById(R.id.btn_save);
this.returnButton = (Button) this.findViewById(R.id.btn_return);
}
protected User buildUserByEditText() {
String firstName = this.userEditText.firstName.getText().toString();
if (firstName == null || TextUtils.isEmpty(firstName)) {
Toast.makeText(this, "姓氏不能为空", Toast.LENGTH_LONG).show();
return null;
}
String lastName = this.userEditText.lastName.getText().toString();
if (lastName == null || TextUtils.isEmpty(lastName)) {
Toast.makeText(this, "名称不能为空", Toast.LENGTH_LONG).show();
return null;
}
User user = new User();
user.setFirstName(firstName);
user.setLastName(lastName);
user.setCompany(this.userEditText.company.getText().toString());
user.setPhone(this.userEditText.phone.getText().toString());
user.setOfficePhone(this.userEditText.officePhone.getText().toString());
user.setFamilyPhone(this.userEditText.familyPhone.getText().toString());
user.setPost(this.userEditText.post.getText().toString());
user.setAddress(this.userEditText.address.getText().toString());
user.setEmail(this.userEditText.email.getText().toString());
user.setRemark(this.userEditText.remark.getText().toString());
user.setImageId(Constant.HEAD_IMGS[imgSelected % Constant.HEAD_IMGS.length]);
return user;
}
/**
* 初始化选择头像对话框
*/
protected void initImgAlertDialog(int selection){
AlertDialog.Builder build = new AlertDialog.Builder(this);
build.setTitle("选择头像图片");
build.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
imageButton.setImageResource(Constant.HEAD_IMGS[imgSelected % Constant.HEAD_IMGS.length]);
}
});
build.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
LayoutInflater layoutInflater = LayoutInflater.from(this);
View view = layoutInflater.inflate(R.layout.imageswitch, null);
Gallery gallery = (Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(selection);
imageSwitcher = (ImageSwitcher) view.findViewById(R.id.img_switcher);
gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
imageSwitcher.setImageResource(Constant.HEAD_IMGS[position % Constant.HEAD_IMGS.length]);
EditTextActivity.this.imgSelected = position;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
imageSwitcher.setFactory(new MyViewFactory(this));
build.setView(view);
this.alertDialog = build.create();
}
/**
* Gallery适配器
*
* @author think
*
*/
private class ImageAdapter extends BaseAdapter{
private Context context;
public ImageAdapter(Context context) {
this.context = context;
}
/**
* 返回显示图片的个数
*/
public int getCount() {
return Integer.MAX_VALUE;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(this.context);
imageView.setImageResource(Constant.HEAD_IMGS[position % Constant.HEAD_IMGS.length]);
imageView.setAdjustViewBounds(true);
imageView.setLayoutParams(new Gallery.LayoutParams(70, 70)); //设置图片大小
imageView.setPadding(15, 10, 15, 10); //设置图片间隔
return imageView;
}
}
private class MyViewFactory implements ViewFactory{
private Context context;
public MyViewFactory(Context context) {
this.context = context;
}
@Override
public View makeView() {
ImageView imageView = new ImageView(this.context);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(90, 90));
return imageView;
}
}
}