1

我想通过. TextView我对如何做到这一点有一个粗略的想法,但不是 100% 确定。我已经查找了多个关于此的教程和问题,但没有一个简单地涵盖了这一点。

这主要是Java我遇到麻烦的事情的一面。我已经设置了我希望代码进入的类,并设置了XML textview以响应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:"));
        }

    };

}
}
4

2 回答 2

9

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!

于 2013-07-10T12:40:59.663 回答
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:"));
于 2013-07-10T12:28:12.017 回答