Java 如何在android中共享editText输入表单actionBar

Java 如何在android中共享editText输入表单actionBar,java,android,Java,Android,我正在尝试使用send action将一些EditText输入从actionBar发送到活动。但是当我共享输入时,文本为空。我无法确定如何执行该操作。EditText是活动的一部分,而不是actionBar的一部分。下面是我的代码,请帮我弄清楚如何执行该操作 MainActivity.java public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action

我正在尝试使用send action将一些EditText输入从actionBar发送到活动。但是当我共享输入时,文本为空。我无法确定如何执行该操作。EditText是活动的一部分,而不是actionBar的一部分。下面是我的代码,请帮我弄清楚如何执行该操作

MainActivity.java

public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is 
   present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    MenuItem menuItem = menu.findItem(R.id.action_share);
    shareActionProvider =
 (ShareActionProvider)MenuItemCompat.getActionProvider(menuItem);
    EditText e1 = (EditText)findViewById(R.id.send);
    String msg = e1.getText().toString();
    setIntent(msg);
    return super.onCreateOptionsMenu(menu);
}

private void setIntent(String text){
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_TEXT,text);
    shareActionProvider.setShareIntent(intent);
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.chandansingh.pizzabits.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="?attr/actionBarSize"
    >

 <EditText
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:id="@+id/send"
     android:text=""
     />

</LinearLayout>



</android.support.design.widget.CoordinatorLayout>

在调用
onCreateOptionsMenu()
时,用户还没有机会与
编辑文本进行交互,因此该文本将为空(或您可能在其中输入的任何默认值)

您可以在
EditText
上调用
addTextChangedListener()
,以了解用户类型,然后根据该值更新
ShareActionProvider
中的
Intent
,如中所示。唯一的活动相当短:

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain   a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS,   WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  Covered in detail in the book _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
 */

package com.commonsware.android.sap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.widget.EditText;
import android.widget.ShareActionProvider;
import android.widget.Toast;

public class MainActivity extends Activity implements
    ShareActionProvider.OnShareTargetSelectedListener, TextWatcher {
  private ShareActionProvider share=null;
  private Intent shareIntent=new Intent(Intent.ACTION_SEND);
  private EditText editor=null;

  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    shareIntent.setType("text/plain");
    editor=(EditText)findViewById(R.id.editor);
    editor.addTextChangedListener(this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.actions, menu);

    share=
        (ShareActionProvider)menu.findItem(R.id.share)
                                 .getActionProvider();
    share.setOnShareTargetSelectedListener(this);

    return(super.onCreateOptionsMenu(menu));
  }

  @Override
  public boolean onShareTargetSelected(ShareActionProvider source,
                                       Intent intent) {
    Toast.makeText(this, intent.getComponent().toString(),
                   Toast.LENGTH_LONG).show();

    return(false);
  }

  @Override
  public void afterTextChanged(Editable s) {
    shareIntent.putExtra(Intent.EXTRA_TEXT, s.toString());
    share.setShareIntent(shareIntent);
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count,
                                int after) {
    // ignored
  }

  @Override
  public void onTextChanged(CharSequence s, int start, int before,
                            int count) {
    // ignored
  }
}