2017-01-12 59 views
1

在我的Android應用程序中,我有listVieweditTexts。我爲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; 
} 
+0

u必須在向上或向下滾動時保存狀態ediitext –

+0

https://dzone.com/articles/list-editable-textboxes –

+0

你嘗試'yourtextboxID.setEnabled(假) '? – Roljhon

回答

0
et.setFocusable(false); 
et.setTextIsSelectable(false); 

//if clickble then true otherwise false 
et.setClickable(true); 
+0

由於可編輯假'editText'不可點擊,所以我無法編輯它。 – David

+0

這個答案太寬泛,不提供解釋。請添加一些關於此解決方案的信息。 – AxelH