Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过短信发送TextView内容_Java_Android_Mobile - Fatal编程技术网

Java 如何通过短信发送TextView内容

Java 如何通过短信发送TextView内容,java,android,mobile,Java,Android,Mobile,我试图从片段中的文本视图中获取数据,并使用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", "@st

我试图从片段中的文本视图中获取数据,并使用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方法的主活动

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>

您不能使用
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

希望有帮助。

这样做:

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);
}
请尝试此代码

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

你有什么问题?有没有收到短信的机会?没有。。。这一行有问题:String text=textview.getText().toString();getText方法不可用,只有getTextColor。我尝试了这个。。。在第四行(intent.putExtra(“sms_body”,TextView.getText().toString());)中,我找不到getText()方法。我看到的所有相关信息都是getTextColor()和getTextColor()。@CodeRo请用代码和xml更新您的问题。我在onCreate()中未注释textView。无法解析textView:@CodeRo您一定犯了其他错误。你导入TextView了吗?并清理构建项目一次。导入android.widget.TextView;我试过这个。。。在第四行(intent.putExtra(“sms_body”,TextView.getText().toString());)中,我找不到getText()方法。我所看到的是getTextColor()和getTextColor()。