Java 如何将编辑文本框中的文本发送到其他应用程序?

Java 如何将编辑文本框中的文本发送到其他应用程序?,java,android,xml,Java,Android,Xml,嗨,我正在尝试开发一个需要从一个编辑文本框发送文本到另一个应用程序(如Kik)的应用程序,我已经制作了共享菜单,我只需要让它从我的编辑文本框发送文本 以下是我的共享菜单代码(XML): 问题是您的代码位于创建选项菜单中,在创建代码时,EditText是空的,因此文本是空的。您需要添加一个侦听器来更改文本。这是完整的代码- public class Decryptor extends Activity { EditText e; private ShareActionProvider mShare

嗨,我正在尝试开发一个需要从一个编辑文本框发送文本到另一个应用程序(如Kik)的应用程序,我已经制作了共享菜单,我只需要让它从我的编辑文本框发送文本

以下是我的共享菜单代码(XML):


问题是您的代码位于创建选项菜单中,在创建代码时,
EditText
是空的,因此
文本
是空的。您需要添加一个侦听器来更改文本。这是完整的代码-

public class Decryptor extends Activity {
EditText e;
private ShareActionProvider mShare;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.decryption);
    e=(EditText)findViewById(R.id.YourID);
    e.addTextChangedListener(commonTextWatcher);


     TextWatcher commonTextWatcher
    = new TextWatcher(){

  @Override
  public void afterTextChanged(Editable s) {
   String text=e.getText.toString();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);
    if (mShare != null) {
    mShare.setShareIntent(shareIntent); 
    } 
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
   // TODO Auto-generated method stub

  }

  @Override
  public void onTextChanged(CharSequence s, int start, int before,
    int count) {
   // TODO Auto-generated method stub

  }};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu2, menu);
    MenuItem shareItem = menu.findItem(R.id.menu_share);
    String text=e.getText.toString();
    mShare = (ShareActionProvider)shareItem.getActionProvider();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);
    mShare.setShareIntent(shareIntent);


    return super.onCreateOptionsMenu(menu);
}
}

我按照你说的做了,没有错误,但它没有“共享”来自的文本it@LochlanKennedy显示代码。你是如何实现我所说的??
package com.example.Encryptor_Kik;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ShareActionProvider;

/**
 * Created with IntelliJ IDEA.
 * Date: 12/12/13
 * Time: 3:15 PM
 * To change this template use File | Settings | File Templates.
 */
public class Decryptor extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.decryption);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu2, menu);
    MenuItem shareItem = menu.findItem(R.id.menu_share);

    ShareActionProvider mShare = (ShareActionProvider)shareItem.getActionProvider();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, "Hi");
    mShare.setShareIntent(shareIntent);


    return super.onCreateOptionsMenu(menu);
}




}
public class Decryptor extends Activity {
EditText e;
private ShareActionProvider mShare;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.decryption);
    e=(EditText)findViewById(R.id.YourID);
    e.addTextChangedListener(commonTextWatcher);


     TextWatcher commonTextWatcher
    = new TextWatcher(){

  @Override
  public void afterTextChanged(Editable s) {
   String text=e.getText.toString();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);
    if (mShare != null) {
    mShare.setShareIntent(shareIntent); 
    } 
  }

  @Override
  public void beforeTextChanged(CharSequence s, int start, int count,
    int after) {
   // TODO Auto-generated method stub

  }

  @Override
  public void onTextChanged(CharSequence s, int start, int before,
    int count) {
   // TODO Auto-generated method stub

  }};
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.menu2, menu);
    MenuItem shareItem = menu.findItem(R.id.menu_share);
    String text=e.getText.toString();
    mShare = (ShareActionProvider)shareItem.getActionProvider();
    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType("text/plain");
    shareIntent.putExtra(Intent.EXTRA_TEXT, text);
    mShare.setShareIntent(shareIntent);


    return super.onCreateOptionsMenu(menu);
}
}