Android共享菜单

时间:2014-12-24 09:40:25

标签: android webview share

我有一个应用程序,其中我有一个WebView,我在其中显示一些网站。 我想在Menu中创建一个Share按钮。我尝试了很多代码。

我应该在这里写什么?

private void action_shareMenuItem(){

// ???????????????我应该在这写什么?

}

MainActivity.java

package com.kefelon.goldplak;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;


public class MainActivity extends ActionBarActivity {
 WebView webview;
    private ShareActionProvider mShareActionProvider;
    ProgressBar loadingProgressBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webview = (WebView) findViewById(R.id.webview);
        webview.getSettings().setSupportZoom(true);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.loadUrl("http://www.goldplak.com");
        webview.setWebViewClient(new WebViewClient() {
        });

        loadingProgressBar=(ProgressBar)findViewById(R.id.progressbar_Horizontal);

        webview.setWebChromeClient(new WebChromeClient() {

            // this will be called on page loading progress

            @Override

            public void onProgressChanged(WebView view, int newProgress) {

                super.onProgressChanged(view, newProgress);


                loadingProgressBar.setProgress(newProgress);
                //loadingTitle.setProgress(newProgress);
                // hide the progress bar if the loading is complete

                if (newProgress == 100) {
                    loadingProgressBar.setVisibility(View.GONE);

                } else{
                    loadingProgressBar.setVisibility(View.VISIBLE);

                }

            }

        });

    }



    @Override
    public void onBackPressed()
    {
        if(webview.canGoBack()){
            webview.goBack();
        }else{
            new AlertDialog.Builder(this)
                    .setIcon(android.R.drawable.ic_dialog_alert)
                    .setTitle("Çıkış")
                    .setMessage("Uygulamadan çıkmak istediğinize emin misiniz?")
                    .setPositiveButton("Evet", new DialogInterface.OnClickListener()
                    {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }

                    })
                    .setNegativeButton("Hayır", null)
                    .show();
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch(item.getItemId()) {
            case R.id.action_about:
                action_aboutMenuItem();
                break;
            case R.id.action_share:
                action_shareMenuItem();
                break;
            case R.id.action_settings:
                action_settingsMenuItem();
                break;
        }
        return true;
    }




    private void action_aboutMenuItem(){
    new AlertDialog.Builder(this)
            .setTitle("Hakkımızda")
            .setMessage("Gold Plak olarak müzik sektörü ile ilgili müşteri odaklı kişisel ve kurumsal hizmet veren, dinamik, güler yüzlü, yaratıcı kadromuzla müşterilerimizin beklentilerini karşılamak için kesintisiz hizmet vermekteyiz.\n" +
                    "\n" +
                    "Bu şekilde müşterilerimizin plak ve dj ekipmanlarından yararlanmalarını sağlayarak taleplerini karşılayabilmek. Hedefimiz, yeni çıkan tüm ürünleri sunmak, maksimum düzeyde müşteri memnuniyetini sağlamak..")
  .setNeutralButton("Tamam", new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int which) {

      }
  }).show();
    }


    private void action_shareMenuItem(){

        //??????????????? What should I write here ?



    }


     private void action_settingsMenuItem(){
    new AlertDialog.Builder(this)
            .setTitle("Versiyon")
            .setMessage("Gold Plak v1.1")
            .setNeutralButton("Tamam", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            }).show();
}


    }

2 个答案:

答案 0 :(得分:1)

您可能需要在项目的res>菜单目录中为共享菜单创建一个xml文件,

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/share"
        android:icon="@drawable/ic_action_share" // share button icon.
        android:showAsAction="always"
        android:title="@string/action_share">
    </item>

</menu>

在你的java文件中,

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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int itemId = item.getItemId();

        if (itemId == R.id.share) {
            shareNews();
        }

        return super.onOptionsItemSelected(item);
    }

这是分享新闻的方法,

    private void shareNews() {
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("text/plain");
        shareIntent.putExtra(Intent.EXTRA_TEXT, "Put Data Here");
        startActivity(Intent.createChooser(shareIntent,getString("How do you want to share")));
    }

答案 1 :(得分:0)

这个问题已经回答了,但无论如何,这是一个有效的解决方案:

片段/活动中的

private ShareActionProvider mShareActionProvider;

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    getActivity().getMenuInflater().inflate(R.menu.deal_detail, menu);

    // Locate MenuItem with ShareActionProviderr
    MenuItem item = menu.findItem(R.id.action_share);

    // Fetch and store ShareActionProvider
    mShareActionProvider = (ShareActionProvider) item.getActionProvider();

    mShareActionProvider.setShareIntent(getShareIntent());
}

// Create the share intent
private Intent getShareIntent() {
    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "yoursubject");
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "yourtext);
    return sharingIntent;
}