【问题标题】:Using ShareActionProvider with Button in Layout在布局中使用带有按钮的 ShareActionProvider
【发布时间】:2014-11-25 06:07:48
【问题描述】:

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

【问题讨论】:

    标签: android shareactionprovider


    【解决方案1】:

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

    Main.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>
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2011-02-26
      • 1970-01-01
      • 2016-05-11
      • 2013-08-06
      • 2011-04-26
      相关资源
      最近更新 更多