在我的Android應用程序中,我有listView
和editTexts
。我爲editTexts
設置textIsSelectable
爲false,但文本仍然保持可選狀態。對於textViews
一切工作正常。當我在適配器中以編程方式執行時,結果相同。EditText textIsSelectable =「false」在列表視圖中不工作
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="@drawable/back"
android:clipToPadding="true"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:layout_marginStart="10dp"
android:elevation="1dp"
android:layout_marginEnd="10dp"
android:padding="10dp"
android:weightSum="10">
<TextView
android:id="@+id/key"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dip"
android:textColor="@color/colorPrimaryDark"
android:textSize="16sp"
android:layout_weight="7"
/>
<EditText
android:id="@+id/value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#aaaaaa"
android:textIsSelectable="false"
android:inputType="none"
android:padding="2dip"
android:textSize="16sp"
android:background="#ffffff"
android:layout_weight="3"/>
<TextView
android:id="@+id/text_value"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="@color/colorPrimaryDark"
android:textIsSelectable="true"
android:autoLink="all"
android:padding="2dip"
android:textSize="16sp"
android:layout_weight="3"
android:visibility="gone"/>
<ImageView
android:id="@+id/img"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:visibility="gone"
android:layout_weight="3"
android:adjustViewBounds="true"
android:clickable="true"/>
</LinearLayout>
</RelativeLayout>
我的自定義適配器。
class MyListViewAdapter extends ArrayAdapter<KeyValueList>
{
private int layoutResource;
private String rowId = "-", string = "-";
private String pos = "0";
private String vallue;
private int i = 0;
MyListViewAdapter(Context context, int layoutResource, List<KeyValueList> keyValueList)
{
super(context, layoutResource, keyValueList);
this.layoutResource = layoutResource;
setDefaultsInt("first", 1, getContext());
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@NonNull
@Override
public View getView(final int position, final View convertView, @NonNull ViewGroup parent)
{
KeyValueList keyValuelist = getItem(position);
View view = convertView;
if (view == null)
{
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
view = layoutInflater.inflate(layoutResource, null);
ViewHolder holder = new ViewHolder();
holder.textView = (TextView) view.findViewById(R.id.key);
holder.editText = (EditText) view.findViewById(R.id.value);
holder.text_value = (TextView) view.findViewById(R.id.text_value);
holder.imageView = (ImageView) view.findViewById(R.id.img);
view.setTag(holder);
}
final ViewHolder holder = (ViewHolder) view.getTag();
if(openEntry.isEditable)
{
holder.text_value.setVisibility(View.GONE);
holder.editText.setVisibility(View.VISIBLE);
}
if(!openEntry.isEditable)
{
holder.text_value.setVisibility(View.VISIBLE);
holder.editText.setVisibility(View.GONE);
}
if (holder.textWatcher != null)
holder.editText.removeTextChangedListener(holder.textWatcher);
holder.textWatcher = new TextWatcher()
{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
}
@Override
public void afterTextChanged(Editable s)
{
if(!s.toString().equals(vallue))
{
if(getDefaultsInt("first",getContext()) == 1)
{
openEntry.edit_list.add(null);
setDefaultsInt("p", position, getContext());
setDefaultsInt("first", 0, getContext());
}
if(getDefaultsInt("p", getContext()) != position)
{
openEntry.edit_list.add(null);
i++;
setDefaultsInt("p", position, getContext());
}
try
{
rowId = getRowID(position);
string = s.toString();
pos = String.valueOf(position);
} catch (JSONException e)
{
e.printStackTrace();
}
HashMap<String, String> edit = new HashMap<>();
edit.put("rowID", rowId);
edit.put("string", string);
edit.put("position", pos);
openEntry.edit_list.set(i, edit);
}
}
};
holder.editText.addTextChangedListener(holder.textWatcher);
assert keyValuelist != null;
holder.textView.setText(keyValuelist.getKey());
vallue = keyValuelist.getValue();
if (vallue.length() != 0 && vallue.contains("printFile"))
{
final String durl = vallue.replace("printFile","file");
new DownloadImageTask(holder.imageView).execute(vallue);
holder.editText.setVisibility(View.GONE);
holder.text_value.setVisibility(View.GONE);
holder.imageView.setVisibility(View.VISIBLE);
holder.imageView.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Uri uriUrl = Uri.parse(durl);
Intent launchBrowser = new Intent(Intent.ACTION_VIEW, uriUrl);
getContext().startActivity(launchBrowser);
}
});
}
else
{
holder.editText.setText(vallue);
holder.text_value.setText(vallue);
}
return view;
}
u必須在向上或向下滾動時保存狀態ediitext –
https://dzone.com/articles/list-editable-textboxes –
你嘗試'yourtextboxID.setEnabled(假) '? – Roljhon