我在旋转器选定值

时间:2016-11-15 18:24:36

标签: java android android-spinner

我试图制作一个应用程序来发送包含默认或用户预定义消息和电话号码的消息我将把这些代码放在这里

 private static final int MY_PERMISSIONS_REQUEST_SEND_SMS = 0;
String phoneNo, message;
List<String> pn, txt;
Spinner sp1, sp2;
Button b, b1, b2, b3, b4;
EditText et, et1;

ArrayAdapter<String> adp1, adp2;

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

    setupSpinners();

    b = (Button) findViewById(R.id.button);
    b1 = (Button) findViewById(R.id.button1);
    b2 = (Button) findViewById(R.id.button2);
    b3 = (Button) findViewById(R.id.button3);
    b4 = (Button) findViewById(R.id.button4);
    et = (EditText) findViewById(R.id.editText);
    et1 = (EditText) findViewById(R.id.editText1);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            adp1.add(et.getText().toString());
            et.setText(null);
        }

    });

    b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            adp2.add(et1.getText().toString());
            et1.setText(null);
        }

    });
}

public void setupSpinners() {

    sp1 = (Spinner) findViewById(R.id.spinner);
    sp2 = (Spinner) findViewById(R.id.spinner1);
    pn = new ArrayList<String>();
    txt = new ArrayList<String>();
    txt.add("Sorry Boss I'cant go today i'm sick");
    pn.add("123456789");

    adp1 = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, pn);
    adp2 = new ArrayAdapter<String>(this,
            android.R.layout.simple_dropdown_item_1line, txt);

    sp1.setAdapter(adp1);
    sp1.setSelection((pn.size() - 1));
    sp2.setAdapter(adp2);
    sp2.setSelection((txt.size() - 1));

    sp1.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            sp1.setSelection(arg2);
        }

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


    });
    sp2.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            sp2.setSelection(arg2);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

            b4.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    sendSMSMessage();
                }

                protected void sendSMSMessage() {
                    phoneNo =  sp1.getSelectedItem().toString();
                    message =  sp2.getSelectedItem().toString();

                    if (ContextCompat.checkSelfPermission(this,
                            Manifest.permission.SEND_SMS)
                            != PackageManager.PERMISSION_GRANTED) {
                        if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                                Manifest.permission.SEND_SMS)) {
                        } else {
                            ActivityCompat.requestPermissions(this,
                                    new String[]{Manifest.permission.SEND_SMS},
                                    MY_PERMISSIONS_REQUEST_SEND_SMS);
                        }
                    }
                }

                @Override
                public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
                    switch (requestCode) {
                        case MY_PERMISSIONS_REQUEST_SEND_SMS: {
                            if (grantResults.length > 0
                                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                                SmsManager smsManager = SmsManager.getDefault();
                                smsManager.sendTextMessage(phoneNo, null, message, null, null);
                                Toast.makeText(getApplicationContext(), "SMS sent.",
                                        Toast.LENGTH_LONG).show();
                            } else {
                                Toast.makeText(getApplicationContext(),
                                        "SMS faild, please try again.", Toast.LENGTH_LONG).show();
                                return;
                            }
                        }
                    }

                }
            });
        }
    });
}

它给出错误的部分是(this,

if (ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
 Manifest.permission.SEND_SMS)) {
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.SEND_SMS},
MY_PERMISSIONS_REQUEST_SEND_SMS);

我需要帮助才能知道如何选择微调器选定的值来发送文本。

2 个答案:

答案 0 :(得分:1)

以下是在spinner中获取所选值的一行代码。

String text = mySpinner.getSelectedItem().toString();

在您的代码中

protected void sendSMSMessage() {
                    phoneNo =  sp1.getSelectedItem().toString();
                    message =  sp2.getSelectedItem().toString();

答案 1 :(得分:0)

pntxt变量为List s,getText()上没有像List这样的方法。