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', в виде обычного текста, который представляет то, что вы хотите отправить. Вы должны сами решить, откуда взялась эта строка, поскольку вы единственный, кто знает, какой текст вы хотите разделить здесь. – CommonsWare

+0

ой, так что действительно невозможно отправить текст, который пользователь набрал? – Kimochis

+0

Нет, это очень возможно. Однако мы не знаем, где пользователь вводит этот текст. Например, ваш исходный код не показывает знаки «EditText». – CommonsWare

ответ

0

intent.putExtra имеет два входа. Первый - это ключ, идентифицирующий строку. Вторая строка сообщения.

Вот пример из 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); 
    } 
} 
Смежные вопросы