[英]Sending bulk of data from one activity to another activity?
朋友,我有2個文本框...用戶必須填寫所有文本框,然后按提交按鈕。 按下提交按鈕后,新的活動應開始,所有填寫的信息應顯示在該處。 我已經使用意圖進行了嘗試,但是第二個活動僅顯示一個文本框的內容。 請告訴我應該怎么做? 提前致謝
package com.example.myfirstapp;
public class MainActivity extends Activity {
static final String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendMessage(View view)
{
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editText1 = (EditText) findViewById(R.id.edit_message2);
String message = editText.getText().toString();
String message2 = editText1.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
intent.putExtra(EXTRA_MESSAGE, message2);
startActivity(intent);
}
}
這是我的第二項活動
package com.example.myfirstapp;
public class DisplayMessageActivity extends Activity {
enter code here
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
// Show the Up button in the action bar.
String message2 = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
TextView textView1 = new TextView(this);
textView1.setTextSize(40);
textView1.setText(message2);
setContentView(textView1);
setupActionBar();
}
/**
* Set up the {@link android.app.ActionBar}, if the API is available.
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
我僅收到第二個文本字段(消息2)的文本...
該怎么辦???
假設您有三個分別具有id的edt1,edt2,edt3的EditText和一個提交按鈕,以及兩個活動,即三個標簽和一個按鈕都駐留在其中的MainActivity和另一個活動,其中三個ID分別為txt1,txt2,txt3的TextView必須顯示信息。
您可以通過以下方式使用意圖將信息從一個活動發送到另一個活動:
intent.putExtra("Key", "Data");
其中Key是您可以從中發送數據的任何字符串,而data是您要發送的信息。 請記住,此密鑰是在另一個活動中接收數據所需要的。
然后,首先需要在MainActivity中找到視圖,然后在“提交”按鈕上單擊偵聽器,您需要執行以下操作:
btnSubmit.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
Intent intent = new Intent(TestActivity.this, AnotherActivity.class);
intent.putExtra("Name", edt1.getText().toString());
intent.putExtra("Phone", edt2.getText().toString());
intent.putExtra("Age", edt3,getText().toString())
startActivity(intent);
}
});
然后,在AnotherActivity中,您需要找到定義的所有TextView的視圖,然后獲取MainActivity發送的數據並將其設置為textview,您需要執行以下操作:
txt1.setText(getIntent.getStringExtra("Name"));
txt2.setText(getIntent.getStringExtra("Phone"));
txt3.setText(getIntent.getStringExtra("Age"));
請記住,密鑰必須與發送數據時指定的密鑰相同。 因此,最好將它們聲明為類的資源字符串或public static final String
字段,而不是嘗試手動使其保持同步。
嘗試此操作以傳遞值。
Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "textbox1");
i.putExtra("Value2", "textbox2");
i.putExtra("Value3", "textbox3");
StartActivity(i);
接收活動
Intent in = getIntent();
String tv1= in.getExtras().getString("Value1");
String tv2= in.getExtras().getString("Value2");
String tv3= in.getExtras().getString("Value3");
獲取文本框的值並將其存儲到一些變量,例如val1
, val2
然后使用
Intent intent = new Intent(getBaseContext(), SecondActivity.class);
intent.putExtra("Textbox_val1", val1);
intent.putExtra("Textbox_val2", val2);
startActivity(intent)
在第二個活動中寫
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value1 = extras.getString(Textbox_val1);
String value2 = extras.getString(Textbox_val2);
}
Intent
putExtra()
,並使用來自不同文本框的不同鍵 intent.getExtras()
並通過鍵檢索信息 您可以使用Intent.putExtra
方法來執行此操作。
例如:
Intent intent = new Intent(this, MyOtherActivity.class);
intent.putExtra("key", "value");
// ... etc
在目標活動的OnResume
或OnStart
中:
Intent intent = getIntent();
String value = intent.getStringExtra("key");
// ... etc
看到:
http://developer.android.com/reference/android/content/Intent.html#getStringExtra(java.lang.String)
有一個有用的方法是共享優先。 通過它您可以創建,編輯,刪除任何活動中的數據,也可以從任何活動中獲取數據。
要在要存儲數據的活動中創建或編輯共享首選項,請執行以下操作:
String share_pref_file = "your_file_name";
SharedPreferences prefs1 = getSharedPreferences(
share_pref_time_file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor1 = prefs1.edit();
editor1.putString("your_data", data); //data is your variable
editor1.commit();
要從活動中獲取數據的位置:
String share_pref_file = "your_file_name";
SharedPreferences prefs = getSharedPreferences(share_pref_file,
Context.MODE_PRIVATE);
String strJson = prefs.getString("your_data", "");
刪除:
String share_pref_file = "your_file_name";
SharedPreferences prefs1 = getSharedPreferences(
share_pref_file, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs1.edit();
editor.remove(share_pref_file);
editor.clear();
editor.commit();
使用ArrayList從一個活動向另一活動發送大量數據的最佳方法之一。
這是一個將完整的arraylist數據發送到其他活動的示例。
http://prashantandroid.blogspot.in/2013/08/passing-arraylist-from-one-activity-to.html
我希望這能幫到您。
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("FirstTxt", textbox1);
intent.putExtra("SecondTxt", textbox2);
intent.putExtra("ThirdTxt", textbox3);
StartActivity(intent);
and for receiving `` extrasIntent in = getIntent();
String first= in.getExtras().getString("FirstTxt");
String second= in.getExtras().getString("SecondTxt");
String third= in.getExtras().getString("ThirdTxt"); it worked for me
很簡單
意圖i =新意圖(this,NewActivity.class);
i.putExtra(“ variable_name”,“ variable_value”); startActivity(i);
它開始第二個活動,您可以從包中找到相同的數據
捆綁包= getIntent()。getExtras();
if(extras!= null){字符串值= extras.getString(“ variable_name”); }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.