简体   繁体   中英

getting text from a textview to display in a string in android

I am trying to create a Share button that once the user presses will allow the user to share to a social networking site.

I have a textview with the text "Chipotle" in my xml and would like the text "Chipotle" to appear in my String Intent. So for example, if the user was to post it onto Facebook it would display "Hey I'm going to Chipotle" and will allow the user to post this onto their wall.

I have done so far this:

placeName = (TextView) findViewById(R.id.place_text);
placeName.getText().toString();
String eShare = "Hey I'm going to" + placeName;

With this I get the following

 I'm going to android.widget.TextView{123212432423....app:id/place_text)

Thanks in advance

You have to assign a variable to this line

placeName.getText().toString();

Like this

placeName = (TextView) findViewById(R.id.place_text);
String placeNameString = placeName.getText().toString();
String eShare = "Hey I'm going to" + placeNameString;

You are not grabbing the text from your textview. It'll work this way:

placeName = (TextView) findViewById(R.id.place_text);
String eShare = "Hey I'm going to " + placeName.getText().toString();

Try using String class as follows

placeName=(TextView) findViewById(R.id.place_text);
String str=placeName.getText().toString();
String eShare="Hey! I am going to "+str;

This will give the desired result ;-)

You need to use .getText().toString() in your assignment too. If you like it in one row:

String eShare = "Hey I'm going to " +
    ((TextView)findViewById(R.id.place_text)).getText().toString();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM