如何将共享按钮添加到android应用程序

如何将共享按钮添加到android应用程序,android,Android,如何将共享按钮添加到我的android应用程序中,我在中尝试了几种解决方案,但似乎没有任何效果 活动\u main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:t

如何将共享按钮添加到我的android应用程序中,我在中尝试了几种解决方案,但似乎没有任何效果

活动\u main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.package.name.MainActivity"
    android:weightSum="1">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toll_bar"
        />

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="368dp"
        android:layout_height="495dp"
        tools:context=".MainActivity"
        tools:layout_editor_absoluteY="8dp"
        tools:layout_editor_absoluteX="8dp">
        tools:ignore="MergeRootFrame">

        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="383dp"
            android:layout_height="match_parent"
            android:layout_below="@+id/toolbar"
            android:layout_marginTop="30dp"
            />
    </FrameLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:elevation="4dp">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:text="@string/app_name"
        android:textAlignment="viewStart"
        android:textAllCaps="true"
        android:textColor="@android:color/white"
        android:textColorHighlight="#000000"
        android:textSize="18sp"
        android:textStyle="bold" />

    <Button
        android:id="@+id/about"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:text="About"
        android:background="@drawable/icon"
        android:layout_marginLeft="127dp"
        />

    <Button
        android:id="@+id/share"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:text="share"
        android:background="@drawable/share"
        android:layout_marginLeft="10dp"
        />

    <Button
        android:id="@+id/rate"
        android:layout_width="45dp"
        android:layout_height="45dp"
        android:text="Rate Us"
        android:background="@drawable/rate"
        android:layout_marginLeft="10dp"
        />

</android.support.v7.widget.Toolbar>
package com.package.name;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;



    @Override
    public void onBackPressed() {
        if(mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar)findViewById(R.id.toolbar);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.loadUrl("http://beta.html5test.com/");
        mWebView.loadUrl("file:///android_asset/Html36289/index.html");
        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());
        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient());

        mWebView .getSettings().setJavaScriptEnabled(true);

        Button btnRate = (Button) findViewById(R.id.rate);
        btnRate.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                onClickRateThisApp(v);
            }});
    }

    private boolean isActivityStarted(Intent aIntent) {
        try
        {
            startActivity(aIntent);
            return true;
        }
        catch (ActivityNotFoundException e)
        {
            return false;
        }
    }

    public void onClickRateThisApp(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
        if (!isActivityStarted(intent)) {
            intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
            if (!isActivityStarted(intent)) {
                Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
            }
        }
    }
    }

谢谢你的回答,我尝试了不同的解决方案,效果很好。 youtube.com/watch?v=U39RsxrWhIA


在我看来,
toll\u bar.xml
是不必要的

首先
活动_main.xml中,请更改

<include
    android:id="@+id/toolbar"
    layout="@layout/toll_bar" />
Third,这里是新的
MainActivity.java

public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;


    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.loadUrl("http://beta.html5test.com/");
        mWebView.loadUrl("file:///android_asset/Html36289/index.html");
        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());
        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient());

        mWebView.getSettings().setJavaScriptEnabled(true);
    }

    private boolean isActivityStarted(Intent aIntent) {
        try {
            startActivity(aIntent);
            return true;
        } catch (ActivityNotFoundException e) {
            return false;
        }
    }

    public void onClickRateThisApp() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
        if (!isActivityStarted(intent)) {
            intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
            if (!isActivityStarted(intent)) {
                Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_about:
                //click the about button
                break;
            case R.id.menu_share:
                //click the share button
                break;
            case R.id.menu_rate:
                onClickRateThisApp();// param view is unnecessary
                break;
        }
        return true;
    }
}

我希望这会对你有所帮助。如果是这样,请采纳我的答案。:-D祝您愉快。

您可以添加此代码,这将对您有所帮助

 Intent share = new Intent(android.content.Intent.ACTION_SEND);
                share.setType("text/plain");
                share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

                                startActivity(Intent.createChooser(share, ""));
这将打开应用程序的多重共享选项。
这有助于我与另一位分享。

请直接在此处发布源代码,而不是链接。谢谢您的回答,我尝试了不同的解决方案,效果很好。
public class MainActivity extends AppCompatActivity {

    Toolbar toolbar;


    @Override
    public void onBackPressed() {
        if (mWebView.canGoBack()) {
            mWebView.goBack();
        } else {
            super.onBackPressed();
        }
    }

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);
        mWebView.loadUrl("http://beta.html5test.com/");
        mWebView.loadUrl("file:///android_asset/Html36289/index.html");
        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient());
        // Stop local links and redirects from opening in browser instead of WebView
        mWebView.setWebViewClient(new MyAppWebViewClient());

        mWebView.getSettings().setJavaScriptEnabled(true);
    }

    private boolean isActivityStarted(Intent aIntent) {
        try {
            startActivity(aIntent);
            return true;
        } catch (ActivityNotFoundException e) {
            return false;
        }
    }

    public void onClickRateThisApp() {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("market://details?id=com.supercell.hayday"));
        if (!isActivityStarted(intent)) {
            intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com.package.name"));
            if (!isActivityStarted(intent)) {
                Toast.makeText(this, "Could not open Android market, please check if the market app installed or not. Try again later", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_about:
                //click the about button
                break;
            case R.id.menu_share:
                //click the share button
                break;
            case R.id.menu_rate:
                onClickRateThisApp();// param view is unnecessary
                break;
        }
        return true;
    }
}
 Intent share = new Intent(android.content.Intent.ACTION_SEND);
                share.setType("text/plain");
                share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);

                                startActivity(Intent.createChooser(share, ""));