共享意图文本输入

时间:2016-08-03 13:49:34

标签: android android-intent sharing

我希望用户能够在单击FAB时共享消息。但是我应该在sendIntent.putExtra(Intent.EXTRA_TEXT, /* what should I put here*/);放置什么?我尝试了消息,但它不起作用。

public class NoteDetailFragment extends Fragment {


public NoteDetailFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View fragmentLayout = inflater.inflate(R.layout.fragment_note_detail, container, false);

    FloatingActionButton fab = (FloatingActionButton)fragmentLayout.findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, /* what should I put here*/);
            sendIntent.setType("text/plain");
            startActivity(sendIntent);
        }
    });

    TextView title = (TextView)fragmentLayout.findViewById(R.id.viewNoteTitle);
    TextView message = (TextView)fragmentLayout.findViewById(R.id.viewNoteMessage);
    TextView thoughts = (TextView)fragmentLayout.findViewById(R.id.viewNoteThoughts);
    ImageView icon = (ImageView)fragmentLayout.findViewById(R.id.viewNoteIcon);

    Intent intent = getActivity().getIntent();

    title.setText(intent.getExtras().getString(MainActivity.NOTE_TITLE_EXTRA));
    message.setText(intent.getExtras().getString(MainActivity.NOTE_MESSAGE_EXTRA));
    thoughts.setText(intent.getExtras().getString(MainActivity.NOTE_THOUGHTS_EXTRA));

    Note.Category noteCat = (Note.Category)intent.getSerializableExtra(MainActivity.NOTE_CATEGORY_EXTRA);
    icon.setImageResource(Note.categoryToDrawable(noteCat));


    return fragmentLayout;
}

}

1 个答案:

答案 0 :(得分:0)

intent.putExtra有两个输入。第一个是标识字符串的键。第二个是消息字符串。

以下是Tutorial

的示例
Exception