gpt4 book ai didi

java - 我在微调器选择的值中遇到错误

转载 作者:行者123 更新时间:2023-12-01 09:15:03 25 4
gpt4 key购买 nike

我正在尝试制作一个应用程序来发送带有默认或用户预定义消息和电话号码的消息,我将在此处放置我的代码

 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);

我需要帮助来了解如何选择微调器选定的值来发送槽文本。

最佳答案

这是一行代码,用于在微调器中获取选定的值。

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

在你的代码中

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

关于java - 我在微调器选择的值中遇到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40617087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com