要实现效果如下:
+ 然后 点击修改按钮后变为
首先在detail,xml里写这个界面的xml
<?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_remark"
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_modify"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
></Button>
<Button
android:id="@+id/btn_delete"
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>
然后,在DetailActivity.java里写
两个方法,一个是所有页面的控件可编辑,一个是不可编辑的
//刚进去后,所有的内容都无法修改。就是对应detail页面,进去后是无法修改的
private void setEditTextDisable(){
et_name.setEnabled(false);
et_mobliePhone.setEnabled(false);
et_familyPhone.setEnabled(false);
et_officePhone.setEnabled(false);
et_position.setEnabled(false);
et_company.setEnabled(false);
et_address.setEnabled(false);
et_zipCode.setEnabled(false);
et_email.setEnabled(false);
et_otherContact.setEnabled(false);
et_remark.setEnabled(false);
btn_img.setEnabled(false);
}
//点击修改后,所有的控件都是可编辑的
private void setEditTextAble(){
et_name.setEnabled(true);
et_mobliePhone.setEnabled(true);
et_familyPhone.setEnabled(true);
et_officePhone.setEnabled(true);
et_position.setEnabled(true);
et_company.setEnabled(true);
et_address.setEnabled(true);
et_zipCode.setEnabled(true);
et_email.setEnabled(true);
et_otherContact.setEnabled(true);
et_remark.setEnabled(true);
btn_img.setEnabled(true);
}
接着,在OnCreate方法里,写单击“修改”按钮的事件(事先要申明一个标识 boolean flag = false;)
setEditTextDisable();
//响应修改按钮事件
btn_modify.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
setEditTextAble();
if(flag){//flag为true时(之前flag初始为false)
flag = false;
btn_modify.setText("修改");
setEditTextDisable();
}else {//flag为false时
flag = true;
btn_modify.setText("保存");
setEditTextAble();
}
}
});
即写这个点击修改按钮的方法后,调用了可编辑的,且button的text改为了保存,获得了上述效果!