在下面的代码中,从第一个活动到第二个活动的意图只携带 1 个字符串,我需要它携带 2 个。我尝试使用 bundle 来实现这一点,但我认为我的问题出在这一行
extras.putString(EXTRA_MESSAGE, message);
您可能会注意到的第二个问题是我需要使用在第一页上输入的字符串来为文本着色,但是我需要花更多时间研究这个问题,第一个问题更重要。
如果有人可以帮助我解决第一个问题,将不胜感激
我有下面的所有相关代码。
主要活动
package com.example.myfirstapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
public final static String EXTRA_MESSAGE_COLOR = "com.example.myfirstapp.MESSAGE2";
@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.activity_main, menu);
return true;
}
/** Called when the user clicks the Send button */
public void sendMessage (View view) {
Intent i = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
EditText editTextcolor = (EditText) findViewById(R.id.edit_message_color);
String message = editText.getText().toString();
String messagecolor = editTextcolor.getText().toString();
i.putExtra(EXTRA_MESSAGE_COLOR, messagecolor);
Bundle extras = new Bundle();
extras.putString(EXTRA_MESSAGE, message);
startActivity(i);
// Do something in response to button
}
}
第二次活动
package com.example.myfirstapp;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import android.widget.TextView;
public class DisplayMessageActivity extends Activity {
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the message from the intent
Bundle bundle = getIntent().getExtras();
String messagecolor = bundle.getString(MainActivity.EXTRA_MESSAGE_COLOR);
String message = bundle.getString(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextColor(getResources().getColor(R.color.blue));
textView.setTextSize(100);
textView.setText(message);
textView.setText(messagecolor);
// Set the text view as the activity layout
setContentView(textView);
// Make sure we're running on Honeycomb or higher to use ActionBar API
if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.HONEYCOMB) {
// Show the Up button in the action bar.
getActionBar().setDisplayHomeAsUpEnabled(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);
}
}
XML 文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical" >
<EditText
android:id="@+id/edit_message"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="@string/edit_message" />
<EditText
android:id="@+id/edit_message_color"
android:layout_weight="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="95dp"
android:layout_marginTop="50dp"
android:textSize="20sp"
android:hint="@string/edit_message_color" />
<Button
android:layout_weight="0"
android:layout_marginLeft="55dp"
android:layout_marginTop="50dp"
android:layout_width="250dp"
android:layout_height="70dp"
android:text="@string/button_send"
android:textColor="@color/royal_blue"
android:textStyle="bold"
android:textSize="30sp"
android:background="@color/pale_green"
android:onClick="sendMessage" />
</LinearLayout>