新建一个工程,名为Contact.
1、看这个图片效果,我们需要这样的xml, 名:addnew.xml
需要说明的是,
1)整个是个LinerLayout,嵌套了两部分,
2)上部分是个ScrollView ,下部分是一个LinerLayout
3)上部分中,又有嵌套了好几个LinerLayout,第一个Linerlayout是姓名处,而且是水平的LinerLayout那(而且两个控件,一个ImageButton和一个EditText), 接下来很多个Linerlayout,每个都有TextView和EditText,是水平排列(如手机的),办公室电话的,等都是Linerlayout,且都是垂直的LinerLayout
4)下部分中,这个LinerLayout有两个Button,是水平的LinerLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ScrollView
android:layout_weight="4"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:scrollbars="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:orientation="horizontal">
<ImageButton
android:id="@+id/btn_img"
android:layout_width="60px"
android:layout_height="60px"
android:src="@drawable/icon"
></ImageButton>
<EditText
android:id="@+id/et_name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="top"
android:layout_gravity="center_vertical"
android:hint="姓名"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="手机"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_mobilephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="手机"
android:phoneNumber="true"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="办公室电话"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_officephone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="办公室电话"
android:phoneNumber="true"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="家庭电话"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_familyphone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="家庭电话"
android:phoneNumber="true"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="职务职称"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_position"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="职务职称"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="单位名称"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_company"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="单位名称"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="地址"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="地址"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="邮政编码"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_zipcode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="邮政编码"
android:phoneNumber="true"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Email"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_email"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="Email"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="其他联系方式"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_other"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="其他联系方式"
></EditText>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="备注"
android:textSize="20dp"
></TextView>
<EditText
android:id="@+id/et_postcode"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="备注"
></EditText>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<Button
android:id="@+id/btn_save"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存"
></Button>
<Button
android:id="@+id/btn_return"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="返回"
></Button>
</LinearLayout>
</LinearLayout>
2、 要下载一些.png的图片,复制到该文件的res的drawable-hpi里
3、 点击姓名旁边的头像,进行修改,其效果为 (名为:imageswitch.xml)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Gallery android:id="@+id/img_gallery"
android:layout_width="fill_parent"
android:layout_height="110px"
android:layout_marginTop="10px"
android:layout_alignParentLeft="true"></Gallery>
<ImageSwitcher android:id="@+id/image_switcher"
android:layout_width="90px"
android:layout_height="90px"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_alignBottom="@+id/img_gallery"></ImageSwitcher>
</RelativeLayout>
注意到有两个Button,这是代码里添加的
4、 新建一个Activity,名为:AddNewActivity,就是增加用户信息,其显示的就是第一个xml的内容
package test.contact;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
public class AddNewActivity extends Activity {
ImageButton btn_img;
AlertDialog imageChooseDialog;
Gallery gallery;
ImageSwitcher is;
int imagePosition;//在图片选择中确定按钮是提交的图片的id
private int[] images={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};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnew);
//实现的主要是修改Image按钮里的东西
btn_img=(ImageButton)this.findViewById(R.id.btn_img);
btn_img.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
initImageChooseDialog(); //当点击选择图片(即名字旁边的图片按钮时的方法)
imageChooseDialog.show();
}
});
}
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) {
// TODO Auto-generated method stub
btn_img.setImageResource(images[imagePosition]);//在选中一张图片后,获得这个图片的id,才能选中它了
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
});
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.imageswitch, null);
//1、Gallery的东西需要显示出来,
gallery = (Gallery) view.findViewById(R.id.img_gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setSelection(images.length/2);//定位中中间的图片
//2、Switcher方面,选中图片
is=(ImageSwitcher) view.findViewById(R.id.image_switcher);
is.setFactory(new MyViewFactory(this));
//1.1 Gallery的响应事件
gallery.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
imagePosition = position;
is.setImageResource(images[position]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
builder.setView(view);
imageChooseDialog = builder.create();
}
//Gallery的Adapter,实现BaseAdapter内部类
class ImageAdapter extends BaseAdapter{
private Context context;
public ImageAdapter(Context context){
this.context=context;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv = new ImageView(context);
iv.setImageResource(images[position]);
iv.setAdjustViewBounds(true);//设置适应视图的大小
iv.setLayoutParams(new Gallery.LayoutParams(80,80));//设置其大小
//设置图片之间的间隔
iv.setPadding(15, 10, 15, 10);
return iv;
}
}
//Switcher的Factory,实现ViewFactory的内部类
class MyViewFactory implements ViewFactory{
private Context context;//需要的上下文
public MyViewFactory(Context context){
this.context=context;
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView iv = new ImageView(context);
iv.setLayoutParams(new ImageSwitcher.LayoutParams(90,90));
return iv;
}
}
}
下次继续使用SQLite以及用javaBean保存联系人信息
未完待续... .........................