从一个活动到另一个活动获取变量

时间:2014-12-17 17:27:00

标签: android android-activity

我使用简单的Android应用程序发送短信。我有2个活动。一个主要用于发送具有2个不同内容的2个不同消息,但消息发送到相同的号码。在secund活动中,我有3个字段:一个用于发送消息的输入号,另外两个用于消息内容。当我单击保存按钮应用程序保存用户输入并返回主要活动。在这里开始我的问题。如何向用户发送数字输入以将消息和消息内容发送到主活动以发送带有已保存用户输入的短信?我是android初学者,所以请帮忙!这是我的MainActivity.java:

  
    

package com.example.davor.light;

  
     

import android.content.Intent;进口   android.support.v7.app.ActionBarActivity; import android.os.Bundle;   import android.telephony.SmsManager;进口   android.telephony.SmsMessage; import android.view.Menu;进口   android.view.MenuItem; import android.view.View;进口   android.widget.Button; import android.widget.ImageView;进口   android.widget.TextView; import android.widget.Toast;

     

公共类MainActivity扩展了ActionBarActivity {

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


    // GUMBI INFORMACIJE
    Button ukljuci = (Button) findViewById(R.id.ukljuci);
    Button iskljuci = (Button) findViewById(R.id.iskljuci);
    Button postavke = (Button) findViewById(R.id.postavke);
    final ImageView slika = (ImageView) findViewById(R.id.slika);

    // INFORMACIJA O PORUCI
    final String broj = "097";
    final String ukljuciPoruka = "Uključi";
    final String iskljuciPoruka = "Isključi";



    // KLIK NA GUMB ISKLJUČI
    iskljuci.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            try {
                SmsManager smsManager = SmsManager.getDefault();
                smsManager.sendTextMessage(broj, null, iskljuciPoruka, null, null);
                Toast.makeText(getApplicationContext(), "Isključeno! poslano na broj " + broj, Toast.LENGTH_LONG).show();
                slika.setImageResource(R.drawable.off);
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(),"Nemoguće isključiti!",Toast.LENGTH_LONG).show();
            }
        }
    });

    postavke.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent PostavkeActivity = new Intent(MainActivity.this,
                    Postavke.class);
            startActivity(PostavkeActivity);
        }
    });
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
} }

这是我想要将用户输入提取到MainActivity的第二个活动的代码:

  

package com.example.davor.light;

     

import android.app.Activity; import android.content.Intent;进口   android.content.SharedPreferences;进口   android.preference.PreferenceManager;进口   android.support.v7.app.ActionBarActivity; import android.os.Bundle;   import android.view.Menu; import android.view.MenuItem;进口   android.view.View; import android.widget.Button;进口   android.widget.EditText; import android.widget.TextView;进口   android.widget.Toast;

     

公共类Postavke扩展了活动{

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

    SharedPreferences loadSettings = PreferenceManager.getDefaultSharedPreferences(this);
    String ucitajBroj = loadSettings.getString("spremiBroj", "");
    String ucitajUkljuci = loadSettings.getString("spremiUkljuci", "");
    String ucitajIskljuci = loadSettings.getString("spremiIskljuci", "");


    final EditText postavkeBroj = (EditText) findViewById(R.id.postavkeBroj);
    postavkeBroj.setText(ucitajBroj);
    final EditText postavkeUkljuci = (EditText) findViewById(R.id.postavkeUkljuci);
    postavkeUkljuci.setText(ucitajUkljuci);
    final EditText  postavkeIskljuci = (EditText) findViewById(R.id.postavkeIskljuci);
    postavkeIskljuci.setText(ucitajIskljuci);

    Button spremi = (Button) findViewById(R.id.postavkeSpremi);
    spremi.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v){
            spremiPostavke("spremiBroj", postavkeBroj.getText().toString());
            spremiPostavke("spremiUkljuci", postavkeUkljuci.getText().toString());
            spremiPostavke("spremiIskljuci", postavkeIskljuci.getText().toString());
            Toast.makeText(getApplicationContext(), "Spremljeno", Toast.LENGTH_LONG).show();
            finish();
        }
    });

}

private void postavkeBroj() {
    EditText postavkeBroj = (EditText) findViewById(R.id.postavkeBroj);
}

private void spremiPostavke (String ključ, String vrijednost) {
    SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString(ključ, vrijednost);
    editor.commit();
}

private void gumbZaPovratak(){
    Button nazad = (Button) findViewById(R.id.nazad);
    nazad.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_postavke, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
} }

4 个答案:

答案 0 :(得分:0)

您可以使用startActivityForResult()

您可以参考此链接: http://hmkcode.com/android-startactivityforresult/

答案 1 :(得分:0)

在Activity1中

Intent myIntent = new Intent(this,Activity2.class);
    myIntent.putExtra("var", variable_to_pass);
    startActivity(myIntent);

在Activity2中

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        intentExtra = extras.getString("var"); // retrieving variable
    }

答案 2 :(得分:0)

您可以将数据添加到这样的意图中:

String messageContent = (EditText) findViewById(yourMessageContentId).getText().toString(); String messageNumber = (EditText) findViewById(yourMessageNumberId).getText().toString(); intent.putExtra("com.example.davor.light.MESSAGE_CONTENT", messageContent); intent.putExtra("com.example.davor.light.MESSAGE_NUMBER", messageNumber);

然后在MainActivity上获取它:

Intent intent = getIntent(); String messageNumber = intent.getStringExtra("com.example.davor.light.MESSAGE_NUMBER"); String messageContent = intent.getStringExtra("com.example.davor.light.MESSAGE_CONTENT");

答案 3 :(得分:0)

您可以将信息从一个活动发送到其他活动,例如bellow

    Intent postavkeActivity = new Intent(MainActivity.this,
            Postavke.class);

  Bundle bundle=new Bundle();
  bundle.putString(|"messageKey1", "message content1");
  bundle.putString(|"messageKey2", "message content2");
   postavkeActivity.putExtras(bundle);

    startActivity(PostavkeActivity);
主活动中的

获取像bellow

这样的包数据

Bundle b = getIntent.getExtras();

String smsContent = b.getString(“messageKey1”);