分享按钮是灰色和残疾人残疾人、按钮、灰色

2023-09-04 03:23:59 作者:壹無所有就是拼的理由

我有以下的code打开共享意图,但它是残疾人和无法点击。

I have the following code to open the share Intent but it's disabled and unable to click.

@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:

share 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>

共享图标在我的行动吧禁用:

The share icon is disabled in my action bar:

推荐答案

这是不是你如何使用 ShareActionProvider

您拨打 setShareIntent() ShareActionProvider 只要你有数据共享。此方法需要一个意图对象重新presenting共享操作 - 基本上你有什么作为共享当地变量。在这一点上,在操作栏上的供应商将被点击,假设有能够基于共享您的设备上的应用你所选择的意图

You call setShareIntent() on the ShareActionProvider as soon as you have the data to share. This method takes an Intent object representing the share operation -- basically what you have as the share local variable. At that point, the provider in the action bar will be clickable, assuming that there are applications on your device capable of sharing based upon your chosen Intent.

另外,你不需要(或希望有)的 R.id.action_share 情况 onOptionsItemSelected(),因为 ShareActionProvider 不需要它,它可能会干扰正常操作。

Also, you do not need (or want to have) the R.id.action_share case in onOptionsItemSelected(), as ShareActionProvider does not need it, and it may interfere with proper operation.

例如,这里是一个实现活性 ShareActionProvider 来共享文本输入到的EditText ,弥补内容视图:

For example, here is an activity that implements a ShareActionProvider to share text entered into the EditText that makes up the content view:

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