2016-09-29 93 views
0

我正在製作一個應用程序,它將在長按之後複製列表視圖中的文本。我在listview中創建了一個字符串數組,現在我想在fragment中實現長按事件。就像當我長按列表項目1時,它應該被複制到剪貼板。我怎樣才能實現它?將文本從列表視圖複製到剪貼板長按

OneFragment.java

import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import com.dragedy.kickasshumor.R; 


public class OneFragment extends Fragment{ 

    public OneFragment() { 
     // Required empty public constructor 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.fragment_one, container, false); 
    } 

} 

FragmentOne.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:entries="@array/list" 
     android:id="@+id/listView" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

</RelativeLayout> 

的strings.xml

<resources> 
    <string-array name="list"> 
     <item>Item 1</item> 
     <item>Item 2</item> 
     <item>Item 3</item> 
     <item>Item 4</item> 
     <item>Item 5</item> 
     <item>Item 5</item> 
     <item>Item 5</item> 
     <item>Item 5</item> 
     <item>Item 5</item> 
    </string-array> 

</resources> 

回答

0

在這裏,我給你一些如何做到這一點

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view = inflater.inflate(R.layout.fragment_one, container, false); 

     // after populating listview here and setting adapter etc. 

     mListView.setOnItemLongClickListener(new OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) { 
       // your method here 
       return true; 
      } 
     }); 

     return view; 
    } 
3

試試這個

ClipData myClip; 
    ClipboardManager clipboard = (ClipboardManager) 
     getSystemService(Context.CLIPBOARD_SERVICE); 
    mListView.setOnItemLongClickListener(new OnItemLongClickListener() { 
     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int pos, long id) { 
      String text = "hello world"; 
      myClip = ClipData.newPlainText("text", text); 
      clipboard.setPrimaryClip(myClip); 
      return true; 
     } 
    }); 
2

使用ClipboardManager你可以從列表視圖行

@Override 
     public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
      super.onActivityCreated(savedInstanceState); 
      ListView listView = (ListView) getView().findViewById(R.id.listview); 
      listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
       @Override 
       public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { 
        return false; 
        // this is textview which you used in listview row 
        TextView textView = (TextView) view.findViewById(R.id.textview); 
        ClipboardManager cm = (ClipboardManager) context 
          .getSystemService(Context.CLIPBOARD_SERVICE); 
        cm.setText(textView.getText().toString()); 
       } 
      }); 

     } 
+0

cm.setText()複製文本已被棄用。 @ febi-mathew的回答是推薦的方法。 – Klox