Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> android 長按彈出菜單,復制,粘貼,全選,android全選

android 長按彈出菜單,復制,粘貼,全選,android全選

編輯:關於android開發

android 長按彈出菜單,復制,粘貼,全選,android全選


<!-- 定義基礎布局LinearLayout -->
<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical">
    <!-- 定義TextView控件 -->
    <EditText
        android:id="@+id/Et"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="長按彈出內容菜單"
        />
</LinearLayout>
package com.example.yanlei.yl2;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

//導入必備的包



public class MainActivity extends AppCompatActivity {

    //定義ContextMenu中每個菜單選項的Id
    final int Menu_1 = Menu.FIRST;
    final int Menu_2 = Menu.FIRST + 1;
    final int Menu_3 = Menu.FIRST + 2;
    EditText Et;
    private ClipboardManager mClipboard = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // 調用父類的onCreate方法
        // 通過setContentView方法設置當前頁面的布局文件為activity_main
        setContentView(R.layout.activity_main);
        //獲得布局中的控件
        Et = (EditText)findViewById(R.id.Et);
        //給EditText注冊上下文菜單
        registerForContextMenu(Et);
    }

    //創建ContextMenu菜單的回調方法
    public void onCreateContextMenu(ContextMenu m, View v,
                                    ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(m,v,menuInfo);

        //在上下文菜單選項中添加選項內容
        //add方法的參數:add(分組id,itemid, 排序, 菜單文字)
        m.add(0, Menu_1, 0, "復制文字");
        m.add(0, Menu_2, 0, "粘貼文字");
        m.add(0, Menu_3, 0, "全選文字");
    }
    private void copyFromEditText1() {

        // Gets a handle to the clipboard service.
        if (null == mClipboard) {
            mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);

        }

        // Creates a new text clip to put on the clipboard
        ClipData clip = ClipData.newPlainText("simple text",Et.getText());

        // Set the clipboard's primary clip.
        mClipboard.setPrimaryClip(clip);
    }
    private void pasteToResult() {
        // Gets a handle to the clipboard service.
        if (null == mClipboard) {
            mClipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        }

        String resultString = "";
        // 檢查剪貼板是否有內容
        if (!mClipboard.hasPrimaryClip()) {
            Toast.makeText(MainActivity.this,
                    "Clipboard is empty", Toast.LENGTH_SHORT).show();
        }
        else {
            ClipData clipData = mClipboard.getPrimaryClip();
            int count = clipData.getItemCount();

            for (int i = 0; i < count; ++i) {

                ClipData.Item item = clipData.getItemAt(i);
                CharSequence str = item
                        .coerceToText(MainActivity.this);
                Log.i("mengdd", "item : " + i + ": " + str);

                resultString += str;
            }

        }
        Toast.makeText(this, resultString, Toast.LENGTH_SHORT).show();
        Et.setText(resultString);
    }


    //ContextMenu菜單選項的選項選擇的回調事件
    public boolean onContextItemSelected(MenuItem item) {
        //參數為用戶選擇的菜單選項對象
        //根據菜單選項的id來執行相應的功能
        switch (item.getItemId()) {
            case 1:
                Toast.makeText(this, "復制文字", Toast.LENGTH_SHORT).show();
                copyFromEditText1();
                break;
            case 2:
                Toast.makeText(this, "粘貼文字", Toast.LENGTH_SHORT).show();
                pasteToResult();
                break;
            case 3:
                Toast.makeText(this, "全選文字", Toast.LENGTH_SHORT).show();
                Et.selectAll();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved