如何获取选定的微调项目并通过短信发送

时间:2012-10-25 04:44:10

标签: android android-spinner

我是Android新手在这里我不知道如何传递选定的微调文本传递给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);
    }

}

// - 必须在此结束

2 个答案:

答案 0 :(得分: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]);
            }

答案 1 :(得分:0)

首先在一个字符串变量中保存所选值,然后发送到短信,另一个选项是在int index函数之外声明onItemSelected()变量,对不起我的英语沟通不好但它会解决你的问题,请参阅以下链接获取更多信息。

Spinners in 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);
    }
}