Android 如何获取选定微调器项目并通过SMS发送

Android 如何获取选定微调器项目并通过SMS发送,android,android-spinner,Android,Android Spinner,我是Android新手,我不知道如何通过按下按钮将选定的微调器文本作为SMS文本发送到选定的号码,以传递给SMS。如果有人能在这里教我,我很高兴 public class MainActivity extends Activity { //all starts here String[] location; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate

我是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);
    }

}
public类MainActivity扩展了活动{//all从这里开始
字符串[]位置;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location=getResources().getStringArray(R.array.location\u数组);
微调器s1=(微调器)findViewById(R.id.spinner1);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u single\u choice,location);
s1.设置适配器(适配器);
s1.setOnItemSelectedListener(新的OnItemSelectedListener()
{
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3)
{
int index=arg0.getSelectedItemPosition();
Toast.makeText(getBaseContext(),“您已选择”+location[index],Toast.LENGTH\u SHORT.show();
}
未选择的公共无效(AdapterView arg0){}
});
}

public void onClick(View v){/首先将所选值保存在一个字符串变量中,然后发送到SMS,另一个选项是declare
int index
Variable out
onItemSelected()
函数,很抱歉我的英语交流不好,但它将解决您的问题,请参阅下面的链接了解更多信息

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

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);
    }
}
public类MainActivity扩展了活动{//all从这里开始
字符串[]位置;
整数指数;
字符串mSelectedItem;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
location=getResources().getStringArray(R.array.location\u数组);
微调器s1=(微调器)findViewById(R.id.spinner1);
ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u single\u choice,location);
s1.设置适配器(适配器);
s1.setOnItemSelectedListener(新的OnItemSelectedListener()
{
已选择公共视图(AdapterView arg0、视图arg1、内部arg2、长arg3)
{
index=arg0.getSelectedItemPosition();
//或者,您也可以使用下面的行存储所选项目。
mSelectedItem=arg0.getSelectedItem().toString();
Toast.makeText(getBaseContext(),“您已选择”+location[index],Toast.LENGTH\u SHORT.show();
}
未选择公共无效(AdapterView arg0){
}
});
}
公共void onClick(视图v){
sendSMS(“5556”,位置[索引]);
//或者你也可以使用下面的代码发送短信。
发送短信(“5556”,mSelectedItem);
}    
//?向其他设备发送SMS消息?
专用void发送短信(字符串电话号码、字符串消息)
{
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(电话号码,空,消息,空,空);
}
}
将此
发送SMS(“5556”、“+位置[索引]”);
放入

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]);
            }
public已选中(AdapterView arg0、View arg1、int arg2、long arg3)
{
Toast.makeText(getBaseContext(),“您已选择”+位置[arg2],Toast.LENGTH\u SHORT.show();
sendSMS(“5556”,位置[arg2]);
}

将此sendSMS(“5556”、“+location[index]”;在公共空间中选择(AdapterView arg0、View arg1、int arg2、long arg3){int index=arg0.getSelectedItemPosition();Toast.makeText(getBaseContext(),“您已选择”+location[index],Toast.LENGTH\u SHORT.show();}@Ken请查看我的答案,如果您对此有任何问题,请告诉我。我已经尝试过,但没有成功。它仍然发送+位置[索引]但是索引被替换为未从微调器中选择的字符menu@Ken现在检查我的编辑实际上我以为你是从参数中获取索引,但事实并非如此,参数int arg2是所选项目的索引。接收方仍然收到不需要的文本,即:+位置[>我正在考虑分配选定的微调器(从字符串数组),然后按下按钮发送sms,接收器应该只接收字符串数组中的值。我不知道如何放置sendSMS()在哪place@Ken现在检查我想你是在选择发送预定义消息,对吗?是的,没错,我正试图传递给sendSMS(),但我完全相信“xxx”下的任何内容将被视为文本Hello Dipak,你能给出代码示例吗?谢谢。我正在努力解决这个问题至少12小时了。谢谢Dipak。声明“int index”全局解决了这个问题。再次感谢!