android - 如何在android ShareActionProvider中共享动态文本

标签 android android-fragments android-actionbar shareactionprovider

我想使用 ShareActionProvider 共享动态文本。我在我的 fragment 中使用这段代码:

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

这将共享进入 Activity 之前的文本,即使我将在按下共享按钮之前在 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

最佳答案

当文本更改时,使用 TextWatcher 更新您的 shareIntentThis sample project有一项 Activity 完全符合您的描述:

/***
  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();我还没有尝试删除它,只是更新了额外的内容。

关于android - 如何在android ShareActionProvider中共享动态文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25575324/

相关文章:

java - 如何在 JSON for 循环中设置 onClick Listener 以检索单击的项目或文本?

android - 引用 Activity 中 fragment 的元素

java - 为什么我在 Android Studio 中不断收到 java.lang.nullpointerException?

Android 复合自定义 View 恢复保存状态

android - actionbar 中的水平进度条宽度

android - 如何在 android studio 3.0 中让 logcat 标签编辑器变大?

android - 通用图像加载器不起作用

android - 如何对齐按钮中的文本

android - Intent.ACTION_VIEW 在可搜索中不起作用

android - 如何动态更改操作栏操作?