2016-08-03 3 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'에 전달해야합니다 : 여기

Tutorial에서 예입니다. 이 문자열이 어디에서 왔는지 직접 결정해야합니다. 여기에서 공유 할 텍스트를 아는 유일한 사람이 있습니다. – CommonsWare

+0

오, 그래서 사용자가 입력 한 텍스트를 보내는 것은 정말 불가능합니다. – Kimochis

+0

아니, 아주 가능합니다. 그러나 사용자가이 텍스트를 어디에 입력하는지는 알 수 없습니다. 소스 코드는'EditText'의 흔적을 보여주지 않습니다. – CommonsWare

답변

0

intent.putExtra에는 2 개의 입력이 있습니다. 첫 번째는 문자열을 식별하는 키입니다. 두 번째는 메시지 문자열입니다. 즉 보내려는 것을 나타냅니다

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