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

这篇博客介绍了如何在Android应用中实现点击按钮,使多个EditText从不可编辑变为可编辑。通过遍历并设置EditText的焦点、触摸模式和启用状态,实现一键切换编辑状态的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这里写图片描述
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文本从不可以编辑到编辑状态。
如有问题,请留言,有更好的方法也可以多多指教。

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值