如何在导航布局中实现共享

时间:2017-10-29 22:55:07

标签: java android xml

我有一个导航应用程序,我想让共享项目工作我使用此代码,但它不起作用

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_download) {
        // Handle the camera action
    } else if (id == R.id.nav_favorite) {

    } else if (id == R.id.nav_allstories) {

    } else if (id == R.id.nav_share) {

        id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(Intent.ACTION_SEND);
                myIntent.setType("text/plain");
                String shareBody="Your Body Here";
                String shareSub="Your Subject Here";
                myIntent.putExtra(Intent.EXTRA_SUBJECT,shareSub);
                myIntent.putExtra(Intent.EXTRA_TEXT,shareBody);
                startActivity(Intent.createChooser(myIntent,"Share using"));

            }
        });

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

我不知道如何才能到达该项目。 我也试过ShareView,但我认为android不支持或类似的东西。

1 个答案:

答案 0 :(得分:0)

public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_download) {
        // Handle the camera action
    } else if (id == R.id.nav_favorite) {

    } else if (id == R.id.nav_allstories) {

    } else if (id == R.id.nav_share) {
        Intent myIntent = new Intent(Intent.ACTION_SEND);
        myIntent.setType("text/plain");
        String shareBody="Your Body Here";
        String shareSub="Your Subject Here";
        myIntent.putExtra(Intent.EXTRA_SUBJECT,shareSub);
        myIntent.putExtra(Intent.EXTRA_TEXT,shareBody);
        startActivity(Intent.createChooser(myIntent,"Share using"));

    } else if (id == R.id.nav_send) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}