Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
431 views
in Technique[技术] by (71.8m points)

java - How to add an item to the text selection popup menu?

When the user marks some text (inside of an EditText, WebView...) a floating text selection popup appears, where apps can add custom items. Can someone please give me an example, how to add an item to this popup menu which makes an intent and transfers the selected String to my activity.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This blog tutorial will show you how: https://medium.com/google-developers/custom-text-selection-actions-with-action-process-text-191f792d2999

Basically, in your Manifest file, add PROCESS_TEXT intent filter to the activity that will handle the text shared from popup menu.

<activity
    android:name=".ProcessTextActivity"
    android:label="@string/process_text_action_name">
  <intent-filter>
    <action android:name="android.intent.action.PROCESS_TEXT" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
  </intent-filter>
</activity>

Then, you would process that text in your Activity like this

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.process_text_main);
  CharSequence text = getIntent()
      .getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT);
  // process the text
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...