关闭

手机通讯录

标签: 手机通讯stringdialoguserbutton
491人阅读 评论(0) 收藏 举报
分类:

手机主界面的UI 01





AddNewActivity

public class AddNewActivity extends Activity {
	ImageButton btn_img;
	AlertDialog imageChooseDialog;
	Gallery gallery; 
	ImageSwitcher is;
	int imagePosition;  
	
	//以下在  自定义方法 inint() 中初始化
	EditText et_name;
	EditText et_mobilePhone;
	EditText et_familyPhone;
	EditText et_officePhone;
	EditText et_position;
	EditText et_company;
	EditText et_address;
	EditText et_zipCode;
	EditText et_email;
	EditText et_otherContact;
	EditText et_remark;
	
	Button btn_save;
	Button btn_return;

	private int[] images = {R.drawable.icon,R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,
							R.drawable.image5,R.drawable.image6,R.drawable.image7,R.drawable.image8,
							R.drawable.image9,R.drawable.image10,R.drawable.image11,R.drawable.image12,
							R.drawable.image13,R.drawable.image14,R.drawable.image15,R.drawable.image16,
							R.drawable.image17,R.drawable.image18,R.drawable.image19,R.drawable.image20,
							R.drawable.image21,R.drawable.image22,R.drawable.image23,R.drawable.image24,};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addnew);
        
        initWidget();
        
        btn_save.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        		String name = et_name.getText().toString();
        		if(name.equals("")) {
        			Toast.makeText(AddNewActivity.this, "姓名不能为空",Toast.LENGTH_LONG).show();
        			return;
        		}
        		String mobilePhone = et_mobilePhone.getText().toString();
				String officePhone = et_officePhone.getText().toString();
				String familyPhone = et_familyPhone.getText().toString();
				String position = et_position.getText().toString();
				String company = et_company.getText().toString();
				String address = et_address.getText().toString();
				String email = et_email.getText().toString();
				String zipCode = et_zipCode.getText().toString();
				String otherContact = et_otherContact.getText().toString();
				String remark = et_remark.getText().toString();
				int imageId = images[imagePosition];
				
				User user = new User();
				user.address = address;
				user.company = company;
				user.email = email;
				user.familyPhone = familyPhone;
				user.imageId = imageId;
				user.mobilePhone = mobilePhone;
				user.officePhone = officePhone;
				user.name = name;
				user.otherContact = otherContact;
				user.position = position;
				user.remark = remark;
				user.zipCode = zipCode;
        			
        			
        			//save user to  database 保存到数据库里面去 
        			long success = DBHelper.getInstance(AddNewActivity.this).save(user);
        			if(success != -1){
        				Toast.makeText(AddNewActivity.this,"添加成功",Toast.LENGTH_LONG).show();        				
        				//1代表 增加用户成功,返回到主页面
        				setResult(1);
        				finish();
        			}else{
        				Toast.makeText(AddNewActivity.this, "添加失败,请重新添加", Toast.LENGTH_LONG).show();
        				//2代表 代表增加用户失败,返回主页面
        				setResult(2);
        				finish();
        			}
        }});
        
        btn_img = (ImageButton)this.findViewById(R.id.btn_img);
        btn_img.setOnClickListener(new OnClickListener(){
        	
        	public void onClick(View v){
        		initImageChooseDialog();
        		imageChooseDialog.show();        		
        	} });
        btn_return.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				setResult(2);  
				finish();				
			}});
    }
    
    public void initWidget(){
    	et_name = (EditText) this.findViewById(R.id.et_name);
		et_mobilePhone = (EditText) this.findViewById(R.id.et_mobilephone);
		et_familyPhone = (EditText) this.findViewById(R.id.et_familyphone);
		et_officePhone = (EditText) this.findViewById(R.id.et_officephone);
		et_position = (EditText) this.findViewById(R.id.et_position);
		et_company = (EditText) this.findViewById(R.id.et_company);
		et_address = (EditText) this.findViewById(R.id.et_address);
		et_zipCode = (EditText) this.findViewById(R.id.et_zipcode);
		et_email = (EditText) this.findViewById(R.id.et_email);
		et_otherContact = (EditText) this.findViewById(R.id.et_other);
		et_remark = (EditText) this.findViewById(R.id.et_remark);
		
		btn_save = (Button) this.findViewById(R.id.btn_save);
		btn_return = (Button) this.findViewById(R.id.btn_return);   
    }
  
	private void initImageChooseDialog() {
		 AlertDialog.Builder builder = new AlertDialog.Builder(this);
		 builder.setTitle("请选择图像");
		 builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				btn_img.setImageResource(images[imagePosition%images.length]);
				
			}
		});
		 builder.setNegativeButton("取消", new DialogInterface.OnClickListener(){
			 public void onClick(DialogInterface dialog, int which){
				 
			 }
		 });
		 
		 LayoutInflater inflater = LayoutInflater.from(this);
		 View view = inflater.inflate(R.layout.imageswitch, null);
		 gallery = (Gallery)view.findViewById(R.id.img_gallery);
		 gallery.setAdapter(new ImageAdapter(this));
		 gallery.setSelection(images.length/2);
		 
		 is = (ImageSwitcher)view.findViewById(R.id.image_switcher);
		 //选择事件成功选择
		 gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
				
			 public void onItemSelected(AdapterView<?> parent, View view,
					 int position, long id){
				 imagePosition = position;
				 is.setImageResource(images[position%images.length]);	 
			 }

			@Override
			public void onNothingSelected(AdapterView<?> parent) {
				// TODO Auto-generated method stub				
			}
		 });
		 
		 is.setFactory(new MyViewFactory(this));
		 builder.setView(view);
		 imageChooseDialog = builder.create();
	}
	
	//ImageAdapter图像适配器 ---内部类
	class ImageAdapter extends BaseAdapter{
		private Context context;
		
		public ImageAdapter(Context context){
			this.context = context;
		}
		
		public int getCount(){
			return Integer.MAX_VALUE;
		}
		
		public Object getItem(int position){
			return null;
		}
		
		public  long getItemId(int position){
			return 0;
		}
				
		public View getView(int position, View convertView, ViewGroup parent){
			ImageView iv = new ImageView(context);
			iv.setImageResource(images[position%images.length]);
			iv.setAdjustViewBounds(true);
			iv.setLayoutParams(new Gallery.LayoutParams(80,80));   //选择头像图标  大小
			iv.setPadding(15,10,15,10);		//图片框之间的间距
			return iv;
		}
		
	}
	
	class MyViewFactory implements ViewFactory{
		
		private  Context context;
		
		public MyViewFactory(Context context){
			this.context = context;
		}
		
		public View makeView(){
			ImageView iv = new ImageView(context);
			iv.setLayoutParams(new ImageSwitcher.LayoutParams(90,90));  //选定框中 选定头像
			return iv;
		}
	}
}
























16_1
















0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:650638次
    • 积分:5840
    • 等级:
    • 排名:第4356名
    • 原创:114篇
    • 转载:174篇
    • 译文:0篇
    • 评论:29条
    最新评论
    114LM