2013-12-12 68 views
0

嗨即時嘗試開發和應用程序需要發送文本從一個編輯文本框到另一個應用程序,如Kik,我已經使共享菜單,我只需要使它從我的編輯文本框發送文本如何將文本從編輯文本框發送到其他應用程序?

我的繼承人份額菜單代碼(XML):

<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:id="@+id/menu_share" 
     android:title="Share" 
     android:orderInCategory="100" 
     android:showAsAction="ifRoom" 
     android:actionProviderClass= "android.widget.ShareActionProvider" /> 

</menu> 

而且繼承人我的java:

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); 
} 




} 

回答

0

的問題是,你的代碼是在onCreateOptionsMenu和創造它的EditText的時間是空的,所以text是空的。您需要添加一個偵聽器來更改文本。這裏是全代碼 -

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); 
} 
} 
+0

我照你說沒有任何錯誤,但它沒有「分享」從它 –

+0

文本@LochlanKennedy顯示的代碼。你怎麼實現我說的? – Naddy

+0

看看編輯。 – Naddy

相關問題