我有一个 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/