长按

时间:2016-09-29 06:49:50

标签: android listview android-studio android-fragments

我正在创建一个应用程序,可以在长按后复制列表视图中的文本。我在listview中创建了一个字符串数组,现在我想在片段中实现长按事件。就像我长按列表项目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>

3 个答案:

答案 0 :(得分:5)

试试这个

$array = array('a', 'b', 'c', 'd');
$array = array_combine(range(1, count($array)), array_values($array));
print_r($array);

答案 1 :(得分:2)

使用ClipboardManager,您可以从listview行复制文本

  @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());
                }
            });

        }

答案 2 :(得分:-1)

以下是如何做到这一点

@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;
    }