編輯:關於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); } }
Android可以通過MediaRecorder和AudioRecord這兩個工具來實現錄音,Me
專門搞android,android在android中,線程可以分為MainThread(UI線程)和WorkerThread(除了MainThread之外的線程)。從原
Android動態加載Activity原理 activity的啟動流程 加載一個Activity肯定不會像加載一般的類那樣,因為activity作為系統的組件有自己的
AlertDialog的六種創建方式,alertdialog六種 AlertDialog的六種創建方式 創建AlertDialog的步驟: 1、創建Aler