安卓官方API DEMO之剪贴板Clipboard

/** * Android系统剪贴板功能DEMO * * @description: * @author ldm * @date 2016-5-16 下午3:15:47 */
public class ClipboardSample extends Activity {
    // 系统剪贴板管理器
    ClipboardManager mClipboard;
    // 下拉列表
    Spinner mSpinner;
    TextView mMimeTypes;
    TextView mDataText;
    CharSequence mStyledText;
    String mPlainText;
    String mHtmlText;
    String mHtmlPlainText;
    TextView tv;
    // 剪切板内容变化的监听器
    ClipboardManager.OnPrimaryClipChangedListener mPrimaryChangeListener = new ClipboardManager.OnPrimaryClipChangedListener() {
        public void onPrimaryClipChanged() {
            updateClipData(true);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.clipboard);
        // 系统剪贴板管理器
        mClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        mStyledText = getText(R.string.styled_text);
        // 初始化界面UI
        initViews();
        // 设置数据及控件监听
        initDataAndEvents();
    }

    private void initDataAndEvents() {
        // <string-array name="clip_data_types">
        // <item>No data in clipboard</item>
        // <item>Text clip</item>
        // <item>HTML Text clip</item>
        // <item>Intent clip</item>
        // <item>Uri clip</item>
        // <item>Coerce to text</item>
        // <item>Coerce to styled text</item>
        // <item>Coerce to HTML text</item>
        // </string-array>
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.clip_data_types,
                android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);
        // Spinner下拉框Item点击事件处理
        mSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view,
                    int position, long id) {
                updateClipData(false);
            }

            public void onNothingSelected(AdapterView<?> parent) {
            }
        });
        mClipboard.addPrimaryClipChangedListener(mPrimaryChangeListener);
        updateClipData(true);
    }

    private void initViews() {
        tv = (TextView) findViewById(R.id.styled_text);
        tv.setText(mStyledText);

        mPlainText = mStyledText.toString();
        tv = (TextView) findViewById(R.id.plain_text);
        tv.setText(mPlainText);

        mHtmlText = "<b>Link:</b> <a href=\"http://www.android.com\">Android</a>";
        mHtmlPlainText = "Link: http://www.android.com";
        tv = (TextView) findViewById(R.id.html_text);
        tv.setText(mHtmlText);

        mSpinner = (Spinner) findViewById(R.id.clip_type);
        mMimeTypes = (TextView) findViewById(R.id.clip_mime_types);
        mDataText = (TextView) findViewById(R.id.clip_text);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mClipboard.removePrimaryClipChangedListener(mPrimaryChangeListener);
    }

    public void pasteStyledText(View button) {
        mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text",
                mStyledText));
    }

    public void pastePlainText(View button) {
        mClipboard.setPrimaryClip(ClipData.newPlainText("Styled Text",
                mPlainText));
    }

    public void pasteHtmlText(View button) {
        mClipboard.setPrimaryClip(ClipData.newHtmlText("HTML Text",
                mHtmlPlainText, mHtmlText));
    }

    public void pasteIntent(View button) {
        Intent intent = new Intent(Intent.ACTION_VIEW,
                Uri.parse("http://www.android.com/"));
        mClipboard.setPrimaryClip(ClipData.newIntent("VIEW intent", intent));
    }

    public void pasteUri(View button) {
        mClipboard.setPrimaryClip(ClipData.newRawUri("URI",
                Uri.parse("http://www.android.com/")));
    }

    /** * 更新剪贴板操作 * */
    void updateClipData(boolean updateType) {
        // 获取剪切板的ClipData数据对象
        ClipData clip = mClipboard.getPrimaryClip();
        // 获取到数据的类型
        String[] mimeTypes = clip != null ? clip.getDescription()
                .filterMimeTypes("*/*") : null;
        if (mimeTypes != null) {
            mMimeTypes.setText("");
            for (int i = 0; i < mimeTypes.length; i++) {
                if (i > 0) {
                    mMimeTypes.append("\n");
                }
                mMimeTypes.append(mimeTypes[i]);
            }
        } else {
            mMimeTypes.setText("NULL");
        }
        // 根据类型来确定Spinner显示的文字
        if (updateType) {
            if (clip != null) {
                ClipData.Item item = clip.getItemAt(0);
                if (item.getHtmlText() != null) {
                    mSpinner.setSelection(2);
                } else if (item.getText() != null) {
                    mSpinner.setSelection(1);
                } else if (item.getIntent() != null) {
                    mSpinner.setSelection(3);
                } else if (item.getUri() != null) {
                    mSpinner.setSelection(4);
                } else {
                    mSpinner.setSelection(0);
                }
            } else {
                mSpinner.setSelection(0);
            }
        }

        if (clip != null) {
            // 获取剪贴板中的数据
            ClipData.Item item = clip.getItemAt(0);
            switch (mSpinner.getSelectedItemPosition()) {
            case 0:
                mDataText.setText("(No data)");
                break;
            case 1:
                mDataText.setText(item.getText());
                break;
            case 2:
                mDataText.setText(item.getHtmlText());
                break;
            case 3:
                // 因没有intent类型数据,会报null
                mDataText.setText(item.getIntent().toUri(0));
                break;
            case 4:
                // 因没有Uri类型数据,会报null
                mDataText.setText(item.getUri().toString());
                break;
            case 5:
                // 剪贴板中的非text数据可以通过ClipData.Item.coerceToText()方法转换为text处理。
                mDataText.setText(item.coerceToText(this));
                break;
            case 6:
                mDataText.setText(item.coerceToStyledText(this));
                break;
            case 7:
                // 转换为Hteml格式
                mDataText.setText(item.coerceToHtmlText(this));
                break;
            default:
                mDataText.setText("Unknown option: "
                        + mSpinner.getSelectedItemPosition());
                break;
            }
        } else {
            mDataText.setText("(NULL clip)");
        }
        // 设置文字监听
        mDataText.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

布局文件:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

    <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" >

        <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >

            <Button  android:id="@+id/copy_styled_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="pasteStyledText" android:text="@string/copy_text" />

            <TextView  android:id="@+id/styled_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="normal" />
        </LinearLayout>

        <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >

            <Button  android:id="@+id/copy_plain_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="pastePlainText" android:text="@string/copy_text" />

            <TextView  android:id="@+id/plain_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="normal" />
        </LinearLayout>

        <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >

            <Button  android:id="@+id/copy_html_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="pasteHtmlText" android:text="@string/copy_text" />

            <TextView  android:id="@+id/html_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="normal" />
        </LinearLayout>

        <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" >

            <Button  android:id="@+id/copy_intent" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="pasteIntent" android:text="@string/copy_intent" />

            <Button  android:id="@+id/copy_uri" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="pasteUri" android:text="@string/copy_uri" />
        </LinearLayout>

        <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:orientation="horizontal" >

            <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Data type: " android:textAppearance="?android:attr/textAppearanceMedium" />

            <Spinner  android:id="@+id/clip_type" android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawSelectorOnTop="true" android:prompt="@string/clip_type_prompt" />
        </LinearLayout>

        <LinearLayout  android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:orientation="horizontal" >
<TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="MIME types: " />

            <TextView  android:id="@+id/clip_mime_types" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" android:background="#ff303030" android:padding="4dp" android:textAppearance="?android:attr/textAppearanceMedium" />

            </LinearLayout>

        <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="4dp" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Data content:" />

        <TextView  android:id="@+id/clip_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ff303030" android:padding="4dp" android:textAppearance="?android:attr/textAppearanceMedium" />

    </LinearLayout>
</ScrollView>

开源代码:https://github.com/ldm520/ANDROID_API_DEMOS

你可能感兴趣的:(安卓官方API DEMO之剪贴板Clipboard)