android - 在布局中将 ShareActionProvider 与按钮一起使用

标签 android shareactionprovider

我有一个带有按钮的布局。单击按钮后,我应该能够获得与“操作栏共享按钮”相同的功能(我们可以使用 ShareActionProvider 实现)。尝试在网络中寻找示例;但找不到。这可能吗?

最佳答案

是的,您可以通过触发隐式 Intent 来响应按钮点击来实现相同的功能。就像下面的例子:

主.java

public class MAIN extends Activity {


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


 }

 public void SHARE(View view) {

    // Do something in response to button

    EditText content = (EditText) findViewById(R.id.editText1);
     String shareBody = content.getText().toString();
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
            sharingIntent.setType("text/plain");
            sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "\n\n");
            sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
            startActivity(Intent.createChooser(sharingIntent,  getResources().getString(R.string.a5)));

 }
}

layout_main.java

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >



            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="31dp"
                android:text="@string/MS1"
                android:gravity="center"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/editText1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="36dp"
                android:ems="10" >

                <requestFocus />
            </EditText>

            <Button
                android:id="@+id/button1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/editText1"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="50dp"
                android:onClick="SHARE"
                android:text="SEND" />

</RelativeLayout>

希望这对您有所帮助。

关于android - 在布局中将 ShareActionProvider 与按钮一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27119857/

相关文章:

java - 来自button.setOnClickListener()的NullPointerException

android - 操作栏中的 ShareActionProvider 图标

android - 带有 ActionBarSherlock 的 ShareActionProvider 只有四个选项

android - 如何在android ShareActionProvider中共享动态文本

android - 触摸屏手势列表和名称

android - 如何制作始终位于顶部的虚拟/软按钮(主页、菜单、后退、搜索)的应用程序/服务?

android - 为什么我得到 Canvas : trying to use a recycled bitmap android. 图形?

android - 重复使用 "findViewById"还是存储View?

android - API 级别低于 14 的 ShareActionProvider

android - 从 ShareActionProvider 列表中删除 YourSelf