通过一个TextView打开的默认短信应用

通过一个TextView打开的默认短信应用

本文介绍了通过一个TextView打开的默认短信应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过一个的TextView 打开默认短信应用。我有我该怎样做一个粗略的想法,但我不是100%确定。我已经看过了多个教程和问题就这个问题和他们的非支付本一样简单,因为它是。

这主要是事情我有麻烦的Java 的一面。我已经设置了我想要的code进去,并建立了在 XML类 的TextView 对<$ C反应$ C> Onclicks 。

只是有与code是什么样子的的Java ,多亏了一点小麻烦。

更新code,在背景,文本,phoneNumber的和smsOnClicklistener得到一个错误。我正的错误是没有任何东西可以被解析为一个变量:

 包com.youtube.iamjackpot;进口android.os.Bundle;
进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.view.Window;
进口android.widget.TextView;公共类InfomenuActivity延伸活动{@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_infomenu);    TextView的textView3 =(的TextView)findViewById(R.id.textView3);
    smsOnClickListener =新View.OnClickListener(){        公共无效的onClick(视图v){
            意图smsIntent =新意图(
                    android.content.Intent.ACTION_VIEW);
            smsIntent.setType(vnd.android-DI​​R / MMS短信);
            smsIntent.putExtra(地址,phoneNumber的);
            smsIntent.putExtra(SMS_BODY,文本);
            context.startActivity(Intent.createChooser(smsIntent,短信:));
        }    };}
}


解决方案

首先,你需要获得的TextView 的保持,你想使点击。通过使用的参考做到这一点的TextView 这样:

  TextView的电视=(的TextView)findViewById(R.id.textViewId);

当你可以设置一个参考 OnClickListener 来,所以,它的点击时,OnClickListener将被解雇了,就像这样:

  tv.setOnClickListener(smsOnClickListener);

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

  smsOnClickListener =新View.OnClickListener(){    公共无效的onClick(视图v){
      意图smsIntent =新意图(android.content.Intent.ACTION_VIEW);
        smsIntent.setType(vnd.android-DI​​R / MMS短信);
        smsIntent.putExtra(地址,phoneNumber的);
        smsIntent.putExtra(SMS_BODY,文本);
        context.startActivity(Intent.createChooser(smsIntent,短信:));
    }

}

祝你好运! :)这是我的第一篇到计算器希望这不会是最后一个!

I would like to open the default SMS app via a TextView. I have a rough idea of how I will do this but am not 100% sure. I have looked up multiple tutorials and questions on this and non of them cover this as simply as it is.

It's mainly the Java side of things I am having trouble with. I have set-up the class I want the code to go in and have set up the XML textview to respond to Onclicks.

just having a little trouble with what the code would look like in Java, thanks.

Updated code, getting an error on "context", "text","phoneNumber" and "smsOnClicklistener". The error I am getting is nothing can be resolved to a variable:

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

    };

}
}
解决方案

First you need to get a hold of the TextView that you want to make clickable. You do this by using the reference of the TextView as such:

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

When you have the reference you can set an OnClickListener to it, so that when it's clicked the OnClickListener will be fired off, like so:

tv.setOnClickListener(smsOnClickListener);

The OnClickListener in your case should look something like:

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

}

Good luck! :) And this is my first post to StackOverFlow hopefully it won't be the last one!

这篇关于通过一个TextView打开的默认短信应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:06