android - 在 ListView 中禁用和启用 edittext

标签 android listview android-edittext

我有一个 ListView 和行布局中的 EditText。我希望 EditText 在创建时被禁用,并使其在单击“编辑”按钮时可编辑。更新文本后,用户可以单击“保存”按钮使其再次不可编辑。我在使相应的 EditText 可编辑时遇到了问题。它已启用但未获得焦点并且软键盘也未出现。我在这里做错了什么吗?

//Adapter class button click:-

 holder.edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.count.setEnabled(true);
                holder.count.setInputType(InputType.TYPE_CLASS_NUMBER);
                holder.count.setFocusable(true);
                holder.count.requestFocus();
                InputMethodManager imm = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(holder.count, InputMethodManager.SHOW_FORCED);
                holder.edit.setVisibility(View.GONE);
                holder.save.setVisibility(View.VISIBLE);
            }
        });
 holder.save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                holder.count.setEnabled(false);
                holder.edit.setVisibility(View.VISIBLE);
                holder.save.setVisibility(View.GONE);
            }
        });
//row layout xml:-

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffb3ff">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">



        <EditText
            android:id="@+id/count"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:ellipsize="end"
            android:padding="10dp"
            android:gravity="center_horizontal"
            android:inputType="none"
            android:enabled="false"
            android:focusable="false"/>

        <Button
            android:id="@+id/edit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="edit"/>

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="save"
            android:visibility="gone"/>
    </LinearLayout>

</FrameLayout>

最佳答案

其实答案很简单。我只需要在我的 list Activity 中添加 android:windowSoftInputMode="stateAlwaysHidden|adjustPan"。之后它与 setEnabled() 一起工作良好。

关于android - 在 ListView 中禁用和启用 edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40039906/

相关文章:

android - 如何为桌面上的任何应用程序创建快捷方式?

android - Android-在没有Maven的情况下安装Mp4Parser

java - 从 Android 中的 Assets 文件夹读取文件时遇到问题

c# - ListView 中的换行符在 Vista/7 中有效,但在 XP 中无效

android - 在这种情况下如何避免 IllegalStateException : The content of the adapter has changed but ListView did not receive a notification

android - Spinner OnItemSelectedListener 不适用于 CustomListAdapter

android - 在 fragment 中的 EditText 中按回车键时修复顶部的工具栏

android - 有什么方法可以用 spannable 替换 EditText 中的文本,但可以节省值(value)?

Android sqlite 数据库 - 我从哪里开始,因为记事本教程已经消失了?

android - 如何使用 Dagger 柄向网络模块添加 retrofit 基本身份验证?