2016-08-03 9 views
0

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; 
} 

} 
+0

あなたが送りたいものを表すプレーンテキストで、 'STRING'に渡す必要があります。あなたがここで共有したいテキストを知っている唯一の人なので、この文字列の出所を自分で決めなければなりません。 – CommonsWare

+0

ああ、ユーザーが入力したテキストを送信することは本当に不可能ですか? – Kimochis

+0

いいえ、それは非常に可能です。ただし、ユーザーがこのテキストをどこに入力しているのかはわかりません。あなたのソースコードは、例えば 'EditText'の兆候を示さない。 – CommonsWare

答えて

0

intent.putExtraには2つの入力があります。最初は文字列を識別するキーです。 2番目はメッセージ文字列です。ここで

Tutorialからの例です:

public class MainActivity extends AppCompatActivity { 
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE"; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    /** Called when the user clicks the Send button */ 
    public void sendMessage(View view) { 
     Intent intent = new Intent(this, DisplayMessageActivity.class); 
     EditText editText = (EditText) findViewById(R.id.edit_message); 
     String message = editText.getText().toString(); 
     intent.putExtra(EXTRA_MESSAGE, message); 
     startActivity(intent); 
    } 
} 
関連する問題