Copy text from TextView on Android

前端 未结 6 1053
野趣味
野趣味 2020-12-02 21:10

I have a ListView where each item is a TextView.

I want to enable the long press behaviour similar to an EditText that display

6条回答
  •  死守一世寂寞
    2020-12-02 22:13

    Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false. My code is here:

    R.layout.edittext.xml

    
    

    ListItemCopyTextActivity.java

    public class ListItemCopyTextActivity extends Activity {    
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout ll = new LinearLayout(this);
        ListView lv = new ListView(this);
    
        String[] values = new String[15];
        for (int i = 0; i < 15; i++) {
            values[i] = "ListItem NO." + i;
        }
    
        ArrayAdapter adapter = new ArrayAdapter(this,
                R.layout.edittext, values);
        lv.setAdapter(adapter);
    
        ll.addView(lv, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    
        setContentView(ll);
    
        }
    }
    

    You can just long click the item, and choose the select text, copy, cut, past etc.

提交回复
热议问题