Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
128 views
in Technique[技术] by (71.8m points)

How to get value from TextView in android?

I'd like to make a simple share button to get a TextView value and share it, but I don't know how to do that.

Here is the code:

public class DetailActivity extends AppCompatActivity {

    Button shareBtn ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        try{
            String key = getIntent().getStringExtra("key");
            String value = getIntent().getStringExtra("value");

            TextView title = (TextView) findViewById(R.id.title);
            title.setText(key);

            TextView body = (TextView) findViewById(R.id.body);
            body.setText(value);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        final Button button = (Button) findViewById(R.id.shareBtn);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                mBody = (TextView)findViewById(R.id.txtBody);  //<--Problme is here
                // Perform action on click
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, mBody);
                sendIntent.setType("text/plain");
                startActivity(sendIntent);
            }
        });

    }    
}

And the relevant layout is:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.naqishop.naqi.DetailActivity"
    tools:showIn="@layout/activity_detail">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Title"
        android:id="@+id/title"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="txtBody"
        android:id="@+id/body"
        android:layout_below="@+id/title"
        android:layout_marginTop="55dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Share this!"
        android:id="@+id/shareBtn"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="76dp"
        android:onClick="shareThis"
        />


</RelativeLayout>

I looked at the docs and some similar question here but could not find my answer. Appreciate your help about this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Do this

public class DetailActivity extends AppCompatActivity {

    Button shareBtn ;
    TextView title,body; //Globally Declaration

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        try{
            String key = getIntent().getStringExtra("key");
            String value = getIntent().getStringExtra("value");

             title = (TextView) findViewById(R.id.title);
            title.setText(key);

             body = (TextView) findViewById(R.id.body);
            body.setText(value);
        }
        catch(Exception e){
            e.printStackTrace();
        }

        final Button button = (Button) findViewById(R.id.shareBtn);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                // Perform action on click
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, title.getText().toString());
                sendIntent.setType("text/plain");
                startActivity(sendIntent);
            }
        });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...