我想在点击到剪贴板上复制listview项目

时间:2016-10-12 06:48:47

标签: android listview android-studio

当用户点击该项目时,我希望将列表视图项目中的复制文本复制到剪贴板,但我仍然坚持在onitemclick函数中使用剪贴板。我该如何实现呢?

public class SmsActivity extends Activity
  implements AdapterView.OnItemClickListener
{
  ListView a;

  String[] c = { "Anniversary SMS", "Best Luck SMS", "Birthday SMS", "Broken Heart SMS", "Education SMS", "Emotion SMS", "Exam SMS" };


  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    a = ((ListView)findViewById(R.id.listView));
    a.setOnItemClickListener(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.title, c);

    a.setAdapter(adapter);


  }

  public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong)
  {

  }
}

2 个答案:

答案 0 :(得分:0)

我希望我的答案对您有所帮助

public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long 
   String s =  a.getItemAtPosition(position)
   ClipboardManager clipboard = (ClipboardManager)CLIPBOARD_SERVICE); 
   ClipData clip = ClipData.newPlainText("label",s);
   clipboard.setPrimaryClip(clip);
  }

答案 1 :(得分:0)

首先获取Clicked项的值,然后放入剪贴板

String clickdData=(String) paramAdapterView.getItemAtPosition(position);   

    ClipboardManager clipboard = (ClipboardManager)
            getSystemService(Context.CLIPBOARD_SERVICE); 
     ClipData clip = ClipData.newPlainText("label", clickdData);
     clipboard.setPrimaryClip(clip);

导入此包 android.content.ClipboardManager 而非 android.text.ClipboardManager