0

我是这里的 Android 新手,我不知道如何通过按下按钮将选定的微调器文本作为 SMS 文本传递给 SMS 以发送到选定的号码。如果有人可以在这里教我,我很高兴。

public class MainActivity extends Activity { //all starts here
    String[] location;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location = getResources().getStringArray(R.array.location_array);
        Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, location);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                int index = arg0.getSelectedItemPosition();
                Toast.makeText(getBaseContext(), "You have selected " + location[index], Toast.LENGTH_SHORT).show();

            }

            public void onNothingSelected(AdapterView<?> arg0){}

        });
    }

        public void onClick(View v) {          //<--**HERE IS THE PROBLEM**
        sendSMS("5556", "+location [index]"); //<--**HERE IS THE PROBLEM**
    }



    //?sends an SMS message to another device?
    private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }

}

//-必须在这里结束

4

2 回答 2

1

把这个放进sendSMS("5556", "+location [index]");

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {

                Toast.makeText(getBaseContext(), "You have selected " + location[arg2], Toast.LENGTH_SHORT).show();
                sendSMS("5556", location[arg2]);
            }
于 2012-10-25T07:33:21.767 回答
0

首先将选定的值保存在一个字符串变量中,然后发送到 SMS 中,另一个选项是在函数int index外声明变量 onItemSelected(),抱歉我的英语交流不好,但它会解决您的问题,请参阅下面的链接以获取更多信息。

Android 中的微调器

并使用下面的代码而不是您的代码。

public class MainActivity extends Activity { //all starts here
    String[] location;
    int index;
    String mSelectedItem;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        location = getResources().getStringArray(R.array.location_array);
        Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, location);
        s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener()
        {
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
            {
                index = arg0.getSelectedItemPosition();
                //OR you can also store selected item using below line.
                mSelectedItem=arg0.getSelectedItem().toString();
                Toast.makeText(getBaseContext(), "You have selected " + location[index], Toast.LENGTH_SHORT).show();
            }

            public void onNothingSelected(AdapterView<?> arg0){

            }

        });
    }

    public void onClick(View v) {
        sendSMS("5556", location [index]);
        //OR you can also send sms using below code.
        sendSMS("5556", mSelectedItem);
    }    

    //?sends an SMS message to another device?
    private void sendSMS(String phoneNumber, String message)
    {
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, null, null);
    }
}
于 2012-10-25T07:33:17.677 回答