android上点击监听器问题发送短信

时间:2015-02-22 14:28:11

标签: android

正确到达我需要按下按钮将编辑文本作为短信发送的点我在onclicklistner之后有新问题,它在行下标记为红色

buttonSms.setOnClickListener  (new OnClickListener()

如果有人能帮助我,我真的很感激

这是完整的代码:

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.app.Activity;
import android.telephony.SmsManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View.OnClickListener;

public class ServiceActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_service);
    //smssending
    Button buttonSms;
    final EditText textsms;
    buttonSms = (Button) findViewById(R.id.buttonSms);
    textsms = (EditText) findViewById(R.id.textsms);
    final String editPhoneNum = "059444444";
    buttonSms.setOnClickListener  (new OnClickListener()
            {
                public void smsClick(View v) {
                    String phoneNo = editPhoneNum;
                    String sms = textsms.getText().toString();
                    try {
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                        Toast.makeText(getApplicationContext(), "SMS Sent!",
                                Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(),
                                "SMS faild, please try again later!",
                                Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }
            }
    );
}

public void backToMain(View view) {
    Intent b = new Intent(this, MainActivity.class);
    startActivity(b);
}

2 个答案:

答案 0 :(得分:0)

您需要@Override onClick方法(您无法重命名)。将您的setOnClickListener方法更改为此

buttonSms.setOnClickListener  (new OnClickListener()
            {
                @Override
                public void onClick(View v)  {
                    String phoneNo = editPhoneNum;
                    String sms = textsms.getText().toString();
                    try {
                        SmsManager smsManager = SmsManager.getDefault();
                        smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                        Toast.makeText(getApplicationContext(), "SMS Sent!",
                                Toast.LENGTH_LONG).show();
                    } catch (Exception e) {
                        Toast.makeText(getApplicationContext(),
                                "SMS faild, please try again later!",
                                Toast.LENGTH_LONG).show();
                        e.printStackTrace();
                    }
                }
            }
    );

答案 1 :(得分:0)

将您的代码更改为此。您使用了smsclick而不是onclick

buttonSms.setOnClickListener (new OnClickListener() { 
public void onClick(View v) { 
String phoneNo = editPhoneNum;
 String sms = textsms.getText().toString(); 
try { 
SmsManager smsManager = SmsManager.getDefault();
 smsManager.sendTextMessage(phoneNo, null, sms, null, null);
 Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
 } catch (Exception e) {
 Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG).show();
 } } ); }