如何实现复制和共享按钮

时间:2017-08-30 11:25:58

标签: java android

如何在底部的每个cardview中添加复制和分享按钮(参见屏幕截图)

以及如何实现复制活动。我尝试了太多方法,但我不知道如何添加复制按钮请给我一些java和XML代码

MainAcitity

public class MainActivity extends AppCompatActivity {
ListView listView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);
    String[] values = new String[]{"Programmer Status", "click 2",
            "click 3", "click 4","click 5",
    };

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, android.R.id.text1, values);

    listView.setAdapter(adapter);

    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            if (position == 0) {
                Intent myIntent = new Intent(view.getContext(), CategoryActivity1.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 1) {
                Intent myIntent = new Intent(view.getContext(), CategoryActivity2.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 2) {
                Intent myIntent = new Intent(view.getContext(), CategoryActivity3.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 3) {
                Intent myIntent = new Intent(view.getContext(), CategoryActivity4.class);
                startActivityForResult(myIntent, 0);
            }
            if (position == 4) {
                Intent myIntent = new Intent(view.getContext(), CategoryActivity5.class);
                startActivityForResult(myIntent, 0);
            }
        }
    });
}
}

Category_1.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schemas.android.com/apk/res-auto"
         android:layout_width="match_parent"
         android:layout_height="match_parent">

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="10dp">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/card_margin">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="16dp"
                android:orientation="vertical"
                android:padding="5dp">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/status_01"/>

            </LinearLayout>

        </android.support.v7.widget.CardView>


    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

CategoryActivity1

public class CategoryActivity1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_1);
}
}

like this

1 个答案:

答案 0 :(得分:0)

尝试此操作以复制剪贴板中的数据

ClipboardManager myClipboard;
myClipboard = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);
ClipData myClip;
String text=textView.getText().toString();
myClip = ClipData.newPlainText("text", text);
myClipboard.setPrimaryClip(myClip);

并使用剪辑剪贴板中的粘贴数据

ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();

将数据共享给其他应用程序的代码

ClipData abc = myClipboard.getPrimaryClip();
ClipData.Item item = abc.getItemAt(0);
String text = item.getText().toString();

 Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
 sharingIntent.setType("text/plain");
 sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
 sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, text);
 startActivity(Intent.createChooser(sharingIntent,"Share with");