从一个活动获取变量到另一个活动
Posted
技术标签:
【中文标题】从一个活动获取变量到另一个活动【英文标题】:Get variables from one activity to another 【发布时间】:2015-02-16 08:11:33 【问题描述】:我使用简单的 android 应用程序来发送短信。我有 2 个活动。一种主要用于发送具有 2 种不同内容的 2 条不同消息,但消息发送到相同的号码。在第二个活动中,我有 3 个字段:一个用于输入号码以发送消息,另外两个用于消息内容。当我单击保存按钮时,应用程序保存用户输入并返回主要活动。这里开始我的问题。我如何向用户发送数字输入以将消息和消息内容发送到主要活动以发送带有保存的用户输入的短信?我完全是android开发的初学者,所以请帮忙!这是我的 MainActivity.java:
包 com.example.davor.light;
导入 android.content.Intent;进口 android.support.v7.app.ActionBarActivity;导入android.os.Bundle; 导入android.telephony.SmsManager;进口 android.telephony.SmsMessage;导入 android.view.Menu;进口 android.view.MenuItem;导入android.view.View;进口 android.widget.Button;导入android.widget.ImageView;进口 android.widget.TextView;导入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 的女巫的第二个活动的代码:
包 com.example.davor.light;
导入android.app.Activity;导入android.content.Intent;进口 android.content.SharedPreferences;进口 android.preference.PreferenceManager;进口 android.support.v7.app.ActionBarActivity;导入android.os.Bundle; 导入 android.view.Menu;导入android.view.MenuItem;进口 android.view.查看;导入android.widget.Button;进口 android.widget.EditText;导入 android.widget.TextView;进口 android.widget.Toast;
公共类 Postavke 扩展 Activity
@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);
【问题讨论】:
【参考方案1】:你可以使用startActivityForResult()
你可以参考这个链接: http://hmkcode.com/android-startactivityforresult/
【讨论】:
【参考方案2】:在活动1中
Intent myIntent = new Intent(this,Activity2.class);
myIntent.putExtra("var", variable_to_pass);
startActivity(myIntent);
在活动2中
Bundle extras = getIntent().getExtras();
if (extras != null)
intentExtra = extras.getString("var"); // retrieving variable
【讨论】:
【参考方案3】:您可以像这样向 Intent 添加数据:
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");
【讨论】:
【参考方案4】:您可以将信息从一项活动发送到其他活动,如下所示
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);
在主要活动中获取如下捆绑数据
捆绑 b=getIntent.getExtras();
String smsContent=b.getString("messageKey1");
【讨论】:
以上是关于从一个活动获取变量到另一个活动的主要内容,如果未能解决你的问题,请参考以下文章