您的位置:首页 > 其它

点击一键切换让文本从不可以编辑到编辑状态

2016-04-17 14:07 429 查看


1.有时候我们app中经常出现一些需求,点击右上角的按钮“编辑”就会变成“完成”,文本里面的EditText从不可以编辑输入到可编辑输入



2.很简单直接上布局

<RelativeLayout
android:id="@+id/parttime_type"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="14dp" >

<TextView
android:id="@+id/tv_parttime_type"
style="@style/publish_jobs_left_title"
android:text="类型" />

<TextView
android:id="@+id/tv_input_parttime_type"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:drawableRight="@drawable/icon_expand_down"
android:gravity="center"
android:text="企业"
android:textColor="@color/new_gray_666666"
android:textSize="16sp" />

<View
android:id="@+id/view1"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:background="@color/hint_gray" />
</RelativeLayout>

<RelativeLayout
android:id="@+id/parttime_title"
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="14dp"
android:focusable="true"
android:focusableInTouchMode="true" >

<TextView
android:id="@+id/tv_parttime_title"
style="@style/publish_jobs_left_title"
android:text="发布方" />

<EditText
android:id="@+id/edt_publisher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toRightOf="@+id/tv_parttime_title"
android:background="@null"
android:gravity="right|center"
android:paddingLeft="14dp"
android:singleLine="true"
android:text="广州吊炸天足浴"
android:textColor="@color/new_gray_666666"
android:textSize="16sp" />

<View
android:id="@+id/view2"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:background="@color/hint_gray" />
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="14dp"
android:focusable="true"
android:focusableInTouchMode="true" >

<TextView
android:id="@+id/tv_contact"
style="@style/publish_jobs_left_title"
android:text="联系人" />

<EditText
android:id="@+id/edt_contact_name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toRightOf="@+id/tv_contact"
android:background="@null"
android:gravity="right|center"
android:paddingLeft="14dp"
android:singleLine="true"
android:text="吊炸天"
android:textColor="@color/new_gray_666666"
android:textSize="16sp" />

<View
android:id="@+id/view3"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:background="@color/hint_gray" />
</RelativeLayout>

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="43dp"
android:layout_marginLeft="14dp"
android:layout_marginRight="14dp"
android:layout_marginTop="14dp"
android:focusable="true"
android:focusableInTouchMode="true" >

<TextView
android:id="@+id/tv_telephone"
style="@style/publish_jobs_left_title"
android:text="手机" />

<EditText
android:id="@+id/edt_telephone_number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="14dp"
android:layout_toRightOf="@+id/tv_telephone"
android:background="@null"
android:gravity="right|center"
android:inputType="number"
android:paddingLeft="14dp"
android:singleLine="true"
android:text="15088888888"
android:textColor="@color/new_gray_666666"
android:textSize="16sp" />

<View
android:id="@+id/view4"
android:layout_width="match_parent"
android:layout_height="1px"
android:layout_alignParentBottom="true"
android:background="@color/hint_gray" />
</RelativeLayout>


3.首先一个思路就要遍历所有的EdictText,点击“编辑”时候让EdictText改变状态

4.全局变量所有EditText

private EditText publishEdt,contactEdt,telephoneEdt,emailEdt,areaEdt,worksEdt;


5.简单的findviewbyid

publishEdt=findMyViewById(R.id.edt_publisher);
contactEdt=findMyViewById(R.id.edt_contact_name);
telephoneEdt=findMyViewById(R.id.edt_telephone_number);
emailEdt=findMyViewById(R.id.edt_email_number);
areaEdt=findMyViewById(R.id.edt_area_detail);
worksEdt=findMyViewById(R.id.edt_works_details);


6.在Onresume里面调用自己定义的一个方法

setEdtList();

@Override
protected void onResume() {
super.onResume();
findById();
setListeners();
setEdtList();
setBgViews();
setEdictale(false);
setBgViewsDeep(true);
}


private void setEdtList() {
edts=new ArrayList<EditText>();
edts.add(publishEdt);
edts.add(contactEdt);
edts.add(telephoneEdt);
edts.add(emailEdt);
edts.add(areaEdt);
edts.add(worksEdt);
}


7.点击状态改变编辑状态

setEdictale(true);和setEdictale(false);

if (edtTv.getText().toString().equals("编辑")) {
edtTv.setText("完成");
setEdictale(true);
setBgViewsDeep(true);
} else if(edtTv.getText().toString().equals("完成")) {
edtTv.setText("编辑");
setEdictale(false);
setBgViewsDeep(false);
}


8.做个循环让之前的edict集合所有做三个方法

-setFocusable

-setFocusableInTouchMode

-setEnabled

private void setEdictale(Boolean edictale){
for (int i = 0; i < edts.size(); i++) {
edts.get(i).setFocusable(edictale);
edts.get(i).setFocusableInTouchMode(edictale);
edts.get(i).setEnabled(edictale);
}
}


自己根据需要传进不同的布尔值类型,现在我们就可以一键解决EdictText文本从不可以编辑到编辑状态。

如有问题,请留言,有更好的方法也可以多多指教。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: