2013-07-10 25 views
1

我想通过TextView打开默认的短信应用程序。我有一个粗略的想法,我将如何做到这一点,但我不是100%肯定。我已经查阅了多个教程和关于这个问题的问题,其中没有一个涵盖了这一点。通过文本视图打开默认的短信应用程序

它主要是我遇到麻烦的事情的一方Java。我建立了我希望代码进入的班级并设置了XMLtextview以响应Onclicks

只是在代码看起来像Java,谢谢。

更新了代码,在“context”,“text”,“phoneNumber”和“smsOnClicklistener”上出现错误。我得到的错误是没有任何东西可以被解析为一个变量:

package com.youtube.iamjackpot; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.Window; 
import android.widget.TextView; 

public class InfomenuActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_infomenu); 

    TextView textView3 = (TextView) findViewById(R.id.textView3); 
    smsOnClickListener = new View.OnClickListener() { 

     public void onClick(View v) { 
      Intent smsIntent = new Intent(
        android.content.Intent.ACTION_VIEW); 
      smsIntent.setType("vnd.android-dir/mms-sms"); 
      smsIntent.putExtra("address", phoneNumber); 
      smsIntent.putExtra("sms_body", text); 
      context.startActivity(Intent.createChooser(smsIntent,"SMS:")); 
     } 

    }; 

} 
} 

回答

8

首先,你需要得到你想要的TextView的保持使点击。

TextView tv = (TextView)findViewById(R.id.textViewId); 

当你有参考你可以设置一个OnClickListener它,所以它被点击时,OnClickListener将被解雇了,就像这样:您可以通过使用TextView的参考这样做

tv.setOnClickListener(smsOnClickListener); 

你的情况应该是这个样子的OnClickListener

smsOnClickListener = new View.OnClickListener() { 

    public void onClick(View v) { 
     Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW); 
     smsIntent.setType("vnd.android-dir/mms-sms"); 
     smsIntent.putExtra("address", phoneNumber); 
     smsIntent.putExtra("sms_body", text); 
     context.startActivity(Intent.createChooser(smsIntent, "SMS:")); 
    } 

}

祝你好运! :)这是我的第一篇文章StackOverFlow希望它不会是最后一个!

+0

当我使用你给我的代码时,我在“上下文”,“文本”,“phoneNumber”和“smsOnClicklistener”上出现错误。我得到的错误是什么都不能解决的变量。我在上面的答案中添加了我的代码,请参阅是否可以看到问题所在,谢谢。 – Jaack

+0

嘿Jaack,谢谢你添加你的代码。变量phoneNumber和文本实际上是变量。所以电话号码应该是一个包含电话号码的字符串,文本将是短信的“主体”。因此,创建两个变量,phoneNumber和包含要发送文本的用户号码的文本以及实际文本。变量上下文实际上就是你从哪里开始你的意图的“上下文”,所以如果你在一个活动中,你可以这个“this”。所以,this.startActivity(Intent.createChooser(smsIntent,“SMS:”))应该这样做.¨¨ – Asbestos

1

文本视图中的onclick执行以下操作:

Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW); 
      smsIntent.setType("vnd.android-dir/mms-sms"); 
      smsIntent.putExtra("address", phno); 
      smsIntent.putExtra("sms_body", body); 
      context.startActivity(Intent.createChooser(smsIntent, "SMS:")); 
相关问题