android将字符串从一个活动传递到另一个活动不起作用,

zzwlnbp8  于 2021-06-30  发布在  Java
关注(0)|答案(3)|浏览(283)

这个问题不太可能帮助任何未来的游客;它只与一个小的地理区域、一个特定的时刻或一个非常狭窄的情况有关,而这些情况通常不适用于互联网的全球受众。有关使此问题更广泛适用的帮助,请访问帮助中心。
7年前关门了。
我想将一个字符串变量从一个活动传递到另一个活动,但不适用于我,我想将字符串从主活动发送到sendsms活动。发送字符串应在sms消息部分中设置。
主活动.java

@Override
    public void onClick(View v) {
        StringBuffer sb = new StringBuffer();

        // Retrive Data from list
        for (Application bean : items) {

            if (bean.isSelected()) {
                sb.append(Html.fromHtml(bean.getContent()));
                sb.append(",");
            }
        }

        showAlertView(sb.toString().trim());

    }

    @SuppressWarnings("deprecation")
    private void showAlertView(String str) {
        AlertDialog alert = new AlertDialog.Builder(this).create();
        final String strContactList = str.substring(0, str.length() - 1);
        if (TextUtils.isEmpty(str)) {
            alert.setTitle("Not Selected");
            alert.setMessage("No One is Seleceted!!!");
        } else {
            // Remove , end of the name

            alert.setTitle("Selected");
            alert.setMessage(strContactList);
        }
        alert.setButton("sms", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                //sendSMS();
                /*Intent sendIntent = new Intent(Intent.ACTION_VIEW);
                sendIntent.putExtra("sms_body", strContactList); 
                sendIntent.setType("vnd.android-dir/mms-sms");
                startActivity(sendIntent);*/
                Intent intent1=new Intent(MainActivity.this,SendSMSActivity.class);
                intent1.putExtra("firstkeyName", strContactList);
                startActivity(intent1);

            }

        });

从主活动发送字符串到sendsmsactivity.java
sendsmsactivity.java文件

public class SendSMSActivity extends Activity {

    Button buttonSend;
    EditText textPhoneNo;
    EditText textSMS;
    String sms;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);

        buttonSend = (Button) findViewById(R.id.buttonSend);
        textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
        textSMS = (EditText) findViewById(R.id.editTextSMS);

        Bundle extras = getIntent().getExtras();
        if(extras !=null) {
             sms = extras.getString("firstkeyName");
        }
        buttonSend.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

              String phoneNo = textPhoneNo.getText().toString();
              try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(phoneNo, null, sms, null, null);
                Toast.makeText(getApplicationContext(), "SMS Sent!",
                            Toast.LENGTH_LONG).show();
              } catch (Exception e) {
                Toast.makeText(getApplicationContext(),
                    "SMS faild, please try again later!",
                    Toast.LENGTH_LONG).show();
                e.printStackTrace();
              }

            }
        });
    }
}

在sendsmsac活动中,我想从main活动获取字符串,它必须设置为sendsms活动的sms主体。我想这样做,但是现在我的代码不起作用,它不能从main获取字符串到sendsmsac活动。

iszxjhcz

iszxjhcz1#

你的临时演员好像有拼写错误。尝试使用“firstkeyname”。应该有用。比如:

sms = extras.getString("firstkeyName");
zhte4eai

zhte4eai2#

这是你问题的答案,试试看。。。

Intent intent1= getIntent(); // gets the previously created intent
final String firstKeyName = intent1.getStringExtra("firstKeyName");
textSMS.setText(firstKeyName);

像这样更改onclick函数

smsManager.sendTextMessage(phoneNo, null, firstKeyName, null, null);
kxkpmulp

kxkpmulp3#

你有firstkeyname和firstkeyname两个不同的键名。i、 e.k是第一个活动的资本。把它作为一个常量&从两个活动中访问它。

相关问题