在下面的代码中,从第一个活动到第二个活动的意图只携带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>
答案 0 :(得分:2)
在布局文件的按钮中添加id属性,并在主活动中的此按钮上实现onClick方法。
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";
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findVIewById(R.id.buttonSend);
b.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
sendMessage();
}
)};
}
@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
}
}
您的布局代码:
<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:id="@+id/buttonSend"
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" />
</LinearLayout>
答案 1 :(得分:1)
试试这个..
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);
i.putExtra(EXTRA_MESSAGE, message);
startActivity(i);
// Do something in response to button
}
答案 2 :(得分:1)
只需将您的sendMessage方法编辑为
即可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);
startActivity(i);
}
答案 3 :(得分:1)
只需将extras
Bundle置于额外的意图中,然后在下一个活动中,您将获得正确的回复
Bundle extras = new Bundle();
extras.putString(EXTRA_MESSAGE, message);
extras.putString(EXTRA_MESSAGE_COLOR, messagecolor );
i.putExtras(extras);
startActivity(i);
答案 4 :(得分:1)
使用getIntent()。getExtras()。getStringArray(“key”)来获取数组。