3

我想使用ShareActionProvider共享動態文本。我使用這個代碼在我的片段:如何在Android ShareActionProvider中共享動態文本

@Override 
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    inflater.inflate(R.menu.detailfragment, menu); 
    MenuItem menuItem = menu.findItem(R.id.action_share); 

    private ShareActionProvider mShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(menuItem); 

    if (mForecast != null) { 
      mShareActionProvider.setShareIntent(createShareForecastIntent()); 
     } 
} 

private Intent createShareForecastIntent() { 
    Intent shareIntent = new Intent(Intent.ACTION_SEND); 
    shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); 
    shareIntent.setType("text/plain"); 
    shareIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString()); 
    return shareIntent; 
} 

這將分享在進入活動之前的文本,即使我會按分享按鈕之前的EditText改變它。而此代碼的工作,因爲我想要的,但我不想做單獨的按鈕分享:

@Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.button: 
       Intent myIntent = new Intent(); 
       myIntent.setAction(Intent.ACTION_SEND); 
       myIntent.putExtra(Intent.EXTRA_TEXT, editText.getText().toString()); 
       myIntent.setType("text/plain"); 
       startActivity(myIntent); 
       break; 
     default: 
       // 
    } 
} 

我使用:

minSdkVersion 14 
targetSdkVersion 19 
+0

嘗試設置在onOptionsItemSelected方法股份意向 – 2014-08-29 20:19:28

+2

不工作;( – 2014-09-07 21:53:46

回答

0

更新您的shareIntent文本更改時,使用TextWatcherThis sample project都有,不只是你描述的活動:

/*** 
    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. 

    From _The Busy Coder's Guide to Android Development_ 
    http://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 
    } 
} 

請注意,我不知道是否需要對每個文本變化setShareIntent()通話;我沒有嘗試刪除,只是更新額外。

+2

我需要從concated一個串2個不同textviews共享文本,我設定了的setText方法的文字,但我的理解TextWatcher不接受它作爲改變,是否有其他解決方案? – 2014-09-07 21:50:11

相關問題