我想在我的Android应用程序中添加"分享"按钮.
像那样

我添加了"分享"按钮,但按钮未激活.我点击了,但没有任何反应.
我在MainActivity.java中的代码:
private ShareActionProvider mShareActionProvider;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.share_menu, menu);
getMenuInflater().inflate(R.menu.main, menu);
MenuItem item = menu.findItem(R.id.share_menu);
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.share_menu).getActionProvider();
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return true;
}
{
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Text");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject");
startActivity(Intent.createChooser(sharingIntent, "Share using"));
}
Run Code Online (Sandbox Code Playgroud)
我想在第一个选项卡(first_tab.xml)或第二个选项卡(second_tab.xml)中共享文本.
选项卡中的代码(xml)(如果需要):
<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"
android:background="@color/background_color"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$DummySectionFragment" >
<TextView
android:id="@+id/section_label1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/text"
android:textColor="@color/text_color" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/sprite" />
Run Code Online (Sandbox Code Playgroud)
https://pub.dev/packages/provider
我可以使用 Provider 向树下的所有小部件全局提供块,而无需使用 MaterialApp 吗?我尝试向页面提供 BLoC,但是当我导航到下一页时,找不到 BloC。那么,每当我导航到新页面时,我是否应该提供它,或者是否有解决方案可以在不使用 MaterialApp 的情况下在全球范围内提供它?
目前我正在这样做
Provider
|_MaterialApp
|_MyPage1 (from which you can navigate to MyPage2...3)
Run Code Online (Sandbox Code Playgroud)
这种方法有效,所有页面都可以访问提供的 BLoC。
但如果使用这种方法
Provider
|_MyPage1 (from which you can navigate to MyPage2...3)
Run Code Online (Sandbox Code Playgroud)
MyPage2、MyPage3 找不到提供的 BLoC。BLoC 只能在 MyPage1 上找到