如何通过短信发送TextView内容

时间:2014-09-24 06:37:28

标签: java android mobile

我正在尝试从Fragment中的TextView获取数据并使用ACTION_SEND发送它。到目前为止,我所能做的就是发送文字/硬编码字符串。

public void implicitSendQuote(View v) {
    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra("sms_body", "@string/quote_1");
    // intent.putExtra(Intent.EXTRA_STREAM, R.id.textView1);
    intent.setType("*/*");
    startActivity(intent);
}

这是包含implicitSendQuote方法的MainActivity

package org.example.androidsdk.demo;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {
    android.app.FragmentManager manager;
    ViewPager viewpager;


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

        // instantiate TextView object
        //TextView textViiew = (TextView) findViewById(R.id.textView1);

        // initialize viewPager
        viewpager = (ViewPager) findViewById(R.id.pager);

        // create object of Adapter class
        PagerAdapter padapter = new org.example.androidsdk.demo.PagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(padapter);

        manager = getFragmentManager();
    }

    public void slideBack() {
        Fragment f1 = new Fragment();
        android.app.FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(R.id.pager, f1, "X");

        // addToBackStack here
        transaction.addToBackStack(null);

        transaction.commit();
    }



    public void implicitSendQuote(View v) {
        Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra("sms_body", "TEXT");
        // intent.putExtra(Intent.EXTRA_STREAM, R.id.textView1);
        intent.setType("*/*");
        startActivity(intent);
    }


}

这是给定活动的XML布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#F20C36" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="62dp"
        android:text="@string/quote_1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="132dp"
        android:layout_marginRight="108dp"
        android:text="Share" 
        android:onClick="implicitSendQuote"/>

</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

您无法使用R.id.textView1获取TextView内容。

//inside onCreate
TextView textview = (TextView) findViewById(R.id.textView1);

// your method to send SMS
public void implicitSendQuote(View v) {
    //get TextView content
    String text = textview.getText().toString();

    Intent intent = new Intent(android.content.Intent.ACTION_SEND);
    intent.setAction(Intent.ACTION_SEND);
    intent.putExtra("sms_body", text);  //use that String text over here.
    intent.setType("*/*");
    startActivity(intent);
}

希望这会有所帮助,您可以清楚地了解如何从视图中获取文本。

修改

在您的代码中,您有以下注释行:

//TextView textViiew = (TextView) findViewById(R.id.textView1);

取消注释。并使用

String text = textViiew.getText().toString();
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setAction(Intent.ACTION_SEND);
intent.putExtra("sms_body", text);  //use that String text over here.
intent.setType("*/*");
startActivity(intent);

在方法implicitSendQuote

希望它有所帮助。

答案 1 :(得分:0)

这样做:

public void implicitSendQuote(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setAction(Intent.ACTION_SEND);
intent.putExtra("sms_body", TextView.getText().toString(););
// intent.putExtra(Intent.EXTRA_STREAM, R.id.textView1);
intent.setType("*/*");
startActivity(intent);
}

答案 2 :(得分:0)

试试这段代码......

Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
            sendIntent.setType("text/plain");
            startActivity(sendIntent);