1

我开发了一个 android 应用程序,其中一个 edittext 用于从联系人列表中选择联系人,一个 edittext 用于键入消息。在我的应用程序中,该应用程序在打开联系人列表并且可以选择一个联系人到 edittext 时运行良好。但是我需要将多个联系人带到edittext ..我必须为此做些什么..我的代码在下面给出..

主要活动

package com.example.sms;

import java.util.StringTokenizer;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity

{

    protected static final int PICK_CONTACT = 0;
    Button btnSendSMS, btnCredit;
    EditText txtPhoneNo, txtMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        btnSendSMS = (Button) findViewById(R.id.btnSendSMS2);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo2);
        txtMessage = (EditText) findViewById(R.id.txtMessage2);
        btnCredit = (Button) findViewById(R.id.button1);






       txtPhoneNo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent it= new Intent(Intent.ACTION_GET_CONTENT);

            it.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);


            startActivityForResult(it, 1);








        }
    });










        btnSendSMS.setOnClickListener(new View.OnClickListener()

        {

            @Override
            public void onClick(View v) 

            {
                // TODO Auto-generated method stub


                String message = txtMessage.getText().toString();
                String phoneNo = txtPhoneNo.getText().toString();

                StringTokenizer st=new StringTokenizer(phoneNo,",");
                while (st.hasMoreElements())

                {

                    String tempMobileNumber = (String)st.nextElement();
                    if(tempMobileNumber.length()>0 && message.trim().length()>0) {
                        sendSMS(tempMobileNumber, message);

                    }


                    else 

                    {

                        Toast.makeText(getBaseContext(), 
                                "Please enter both phone number and message.", 
                                Toast.LENGTH_SHORT).show();
                    }



            }


       }
            });

        btnCredit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub


                Intent i = new Intent(MainActivity.this, credits.class );
                startActivity(i);

            }
        });

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (data != null) {
            Uri uri = data.getData();

            if (uri != null) {
                Cursor c = null;
                try {
                    c = getContentResolver().query(uri, new String[]{ 
                                ContactsContract.CommonDataKinds.Phone.NUMBER,  
                                ContactsContract.CommonDataKinds.Phone.TYPE },
                            null, null, null);

                    if (c != null && c.moveToFirst()) {
                        String number = c.getString(0);
                        int type = c.getInt(1);
                        showSelectedNumber(type, number);
                    }
                } finally {
                    if (c != null) {
                        c.close();
                    }
                }
            }
        }
    }

    public void showSelectedNumber(int type, String number) {
        //Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show(); 
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo2);
        txtPhoneNo.setText(number);

    }




        private void sendSMS(String phoneNumber, String message)
        {
            String SENT = "SMS_SENT";
            String DELIVERED = "SMS_DELIVERED";

            PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
                new Intent(SENT), 0);

            PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
                new Intent(DELIVERED), 0);

          //---when the SMS has been sent---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS sent", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                            Toast.makeText(getBaseContext(), "Generic failure", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NO_SERVICE:
                            Toast.makeText(getBaseContext(), "No service", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_NULL_PDU:
                            Toast.makeText(getBaseContext(), "Null PDU", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case SmsManager.RESULT_ERROR_RADIO_OFF:
                            Toast.makeText(getBaseContext(), "Radio off", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            },new IntentFilter(SENT));

            //---when the SMS has been delivered---
            registerReceiver(new BroadcastReceiver(){
                @Override
                public void onReceive(Context arg0, Intent arg1) {
                    switch (getResultCode())
                    {
                        case Activity.RESULT_OK:
                            Toast.makeText(getBaseContext(), "SMS delivered", 
                                    Toast.LENGTH_SHORT).show();
                            break;
                        case Activity.RESULT_CANCELED:
                            Toast.makeText(getBaseContext(), "SMS not delivered", 
                                    Toast.LENGTH_SHORT).show();
                            break;                        
                    }
                }
            }, new IntentFilter(DELIVERED));        

            SmsManager sms = SmsManager.getDefault();
            sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);       


        }








}
4

2 回答 2

1

将您的方法更改showSelectedNumber为:

public void showSelectedNumber(int type, String number) {
    // Toast.makeText(this, type + ": " + number, Toast.LENGTH_LONG).show(); 
    // txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo2);
    if(txtPhoneNo != null && txtPhoneNo.getText().toString().length()==0)
         txtPhoneNo.setText(number);
    else
         if(txtPhoneNo != null) txtPhoneNo.append(","+number);

}
于 2013-09-13T10:53:31.920 回答
0

在 textview 上调用 settext() 之前,您需要将所选数字添加到字符串中。

像这样的东西:

String str ="number1"+"number2"+....+"numberN"
txtphonenO.setText(str);
于 2013-09-13T10:52:39.807 回答