2014-09-24 24 views
-2

我試圖從片段中的TextView中獲取數據並使用ACTION_SEND發送它。到目前爲止,我所能做的就是發送一個文字/硬編碼的字符串。如何通過短信發送TextView內容

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


} 

在MainActivity這是給定的活動

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

你有什麼問題? – pskink 2014-09-24 06:44:50

+0

http://developer.android.com/guide/topics/resources/string-resource.html#String – raja 2014-09-24 06:51:09

+0

獲得文本的運氣? – 2014-09-25 10:05:04

回答

0

不能用於獲取TextView的內容使用R.id.textView1的XML佈局。

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

希望它能幫助。

+0

我試過這個...在第四行(intent.putExtra(「sms_body」,TextView.getText()。toString());)我找不到getText()方法。我看到的所有相關的是getTextColor()和getTextColors()。 – CodeRo 2014-09-25 03:44:51

+0

@CodeRo請用代碼和xml更新你的問題。 – 2014-09-25 04:18:13

+0

我取消了onCreate()中的textView。 textView無法解析: – CodeRo 2014-09-25 05:00:21

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

我試過這個...在第四行(intent.putExtra(「sms_body」,TextView.getText()。toString());)我找不到getText()方法。我看到的所有相關的是getTextColor()和getTextColors()。 – CodeRo 2014-09-25 03:45:18

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