Android Intent 不携带一束字符串

标签 android android-intent android-button

在下面的代码中,从第一个 Activity 到第二个 Activity 的 Intent 只携带 1 个字符串,我需要它携带 2 个字符串。我尝试使用 bundle 来实现这一点,但我认为我的问题出在这一行

extras.putString(EXTRA_MESSAGE, message);

您可能会注意到的第二个问题是我需要使用在第一页上输入的字符串来为文本着色,但是我需要花更多时间研究这个问题,而第一个问题更为重要。

如果有人能帮我解决第一个问题,我将不胜感激

我在下面有所有相关代码。

主要 Activity

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

}

}

第二个 Activity

 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>

最佳答案

只需将 extras Bundle 放入 intent extra 中,然后在下一个 Activity 中您将获得正确的响应

    Bundle extras = new Bundle();
    extras.putString(EXTRA_MESSAGE, message);
    extras.putString(EXTRA_MESSAGE_COLOR, messagecolor );
    i.putExtras(extras);
    startActivity(i);

关于Android Intent 不携带一束字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14895363/

相关文章:

android - 找不到处理 Intent { } 的 Activity

Android 布局作为具有可选功能的按钮

android - 使用 XML 布局作为模板以编程方式创建 android 按钮

android - 当我返回事件时触发事件

android - java.lang.NoSuchMethodError : No virtual method setSkipClientToken(Z)V in class Lcom/facebook/GraphRequest;

java - 列表未更新的应用程序小部件 - 为什么会这样?

Android 房间没有正确删除缓存

android - 自定义 View 或膨胀布局?

android - 服务启动的 Intent 过滤器

android - 启动新 Activity 时发生 TransactionTooLargeException