图像作为共享按钮

时间:2013-12-16 13:36:27

标签: android share imagebutton

如何在图像按钮上调用ShareActionProvider点击android?我想分享我的应用程序的链接(PlayStore),如果它不可能,如何使用图像btton作为分享按钮?我不想实现动作栏..下面是我的代码

 <ImageButton
                android:id="@+id/imageButton1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/share" />
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainMenu
extends Activity{

//code for displaying random images     *****************************************************************
final Random rnd = new Random();

@Override

protected void onCreate(
    final Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main_menu);

    final ImageView img = (ImageView) findViewById(R.id.imgRandom);

    final String str = "img_" + rnd.nextInt(9);
    img.setImageDrawable
    (
        getResources().getDrawable(getResourceID(str, "drawable",
            getApplicationContext()))
        );
}

protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
    final int ResourceID =
        ctx.getResources().getIdentifier(resName, resType,
            ctx.getApplicationInfo().packageName);
    if (ResourceID == 0)
    {
        throw new IllegalArgumentException
        (
            "No resource string found with name " + resName
            );
    }
    else
    {
        return ResourceID;
    }
}
       //*****************************************************************************************    ****************

@Override
public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.main_menu, menu);
  return true;


ImageButton btn=(ImageButton)findViewById(R.id.imgbutton1);
    btn.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            try {
                startActivity(new Intent(Intent.ACTION_VIEW,     Uri.parse("market://details?id="+com.nullsoft.prostub")));"
            } catch (android.content.ActivityNotFoundException anfe) {
                startActivity(new Intent(Intent.ACTION_VIEW,     Uri.parse("http://play.google.com/store/apps/details?id="+com.nullsoft.prostub")));
            }// TODO Auto-generated method stub

        }

        @Override
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub

        }
    });
}
public void openNewActivity(View view) { 
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);//button to open main
    }
        public void browser(View view) { 
            Intent intent = new Intent(this,Browser.class);
            startActivity(intent);//button to open browser
    }
        public void OpenBrowser(View view) { 
            Intent intent = new Intent(this,Browser.class);
            startActivity(intent);//button to open browser
    }




        {
}
}

1 个答案:

答案 0 :(得分:0)

import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;

public class First extends Activity {

    // code for displaying random images
    // *****************************************************************
    final Random rnd = new Random();

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main_menu);


 ImageButton btn = (ImageButton) findViewById(R.id.button1);
            btn.setOnClickListener(new View.OnClickListener() {

                public void onClick(View v) {
                    try {
                        startActivity(new Intent(Intent.ACTION_VIEW, Uri
                                .parse("market://details?id="
                                        + "com.nullsoft.prostub")));
                    } catch (android.content.ActivityNotFoundException anfe) {
                        startActivity(new Intent(
                                Intent.ACTION_VIEW,
                                Uri.parse("http://play.google.com/store/apps/details?id="
                                        + "com.nullsoft.prostub")));
                    }// TODO Auto-generated method stub

                }
            });
        final ImageView img = (ImageView) findViewById(R.id.imgRandom);

        final String str = "img_" + rnd.nextInt(9);
        img.setImageDrawable(getResources().getDrawable(
                getResourceID(str, "drawable", getApplicationContext())));
    }

    protected final static int getResourceID(final String resName,
            final String resType, final Context ctx) {
        final int ResourceID = ctx.getResources().getIdentifier(resName,
                resType, ctx.getApplicationInfo().packageName);
        if (ResourceID == 0) {
            throw new IllegalArgumentException(
                    "No resource string found with name " + resName);
        } else {
            return ResourceID;
        }
    }

    // *****************************************************************************************
    // ****************

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_menu, menu);
        return true;


    }
public void openNewActivity(View view) { 
        Intent intent = new Intent(this,MainActivity.class);
        startActivity(intent);//button to open main
    }
        public void browser(View view) { 
            Intent intent = new Intent(this,Browser.class);
            startActivity(intent);//button to open browser
    }
        public void OpenBrowser(View view) { 
            Intent intent = new Intent(this,Browser.class);
            startActivity(intent);//button to open browser
    }
}