我试图在android studio中设置一个分享按钮。当我单击操作栏上的共享按钮时,没有任何反应。这是它的样子 menu_main xml:
<item
android:id="@+id/action_share"
android:icon="@drawable/sharebutton"
android:title="@string/abc_shareactionprovider_share_with"
app:showAsAction="ifRoom"/>
以下是MainActivity中的java代码:
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_share) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Download this App!. ");
sendIntent.setType("text/plain");
return true;
}
return super.onOptionsItemSelected(item);
}
在应用程序本身中,单击操作栏上的按钮时没有任何反应。
答案 0 :(得分:0)
你省略了startActivity(sendIntent);
if (id == R.id.action_share) {
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "Download this App!. ");
sendIntent.setType("text/plain");
startActivity(sendIntent);
return true;
}