如何从android中的TextView获取价值? [英] How to get value from TextView in android?

查看:37
本文介绍了如何从android中的TextView获取价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个简单的分享按钮来获取 TextView 值并分享它,但我不知道该怎么做.

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.

代码如下:

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

    }    
}

而相关的布局是:

<?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>

我在这里查看了 docs 和一些类似的问题,但找不到我的答案.感谢您对此的帮助.

I looked at the docs and some similar question here but could not find my answer. Appreciate your help about 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);
            }
        });

这篇关于如何从android中的TextView获取价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆