“共享”按钮显示为灰色并已禁用

时间:2013-11-08 18:56:41

标签: java android android-intent

我有以下代码来打开共享Intent但它被禁用且无法点击。

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.share, menu);

        return true;
    }
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home: 
                onBackPressed();
                break;
            case R.id.action_share:
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/png");
                File folder = new File(Environment.getExternalStorageDirectory() + "/TESTFOLDER");
                if (!folder.exists()) {
                    folder.mkdir();
                }
                try {
                    if (file.exists()) {
                        share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                        startActivityForResult(Intent.createChooser(share, "Share Chart"), 1);
                    }
                    else {
                        //displayToast("Please save your image before sharing.");
                    }
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
        return true;
    }

分享xml:

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

我的操作栏中禁用了共享图标:

enter image description here

1 个答案:

答案 0 :(得分:1)

这不是您使用ShareActionProvider的方式。

只要您有共享数据,就可以在setShareIntent()上致电ShareActionProvider。此方法采用表示共享操作的Intent对象 - 基本上就是share局部变量。此时,操作栏中的提供程序将是可点击的,假设您的设备上的应用程序能够根据您选择的Intent进行共享。

此外,您R.id.action_share中不需要(或想要)onOptionsItemSelected()个案,因为ShareActionProvider不需要它,并且可能会影响正常操作。

例如,以下是一个活动,它实现ShareActionProvider以共享输入到构成内容视图的EditText中的文本:

/***
  Copyright (c) 2013 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, editor.getText());
    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
  }
}