当前位置: 动力学知识库 > 问答 > 编程问答 >

android - How to display text on the second activity

问题描述:

So the first activity asks user to click on one out of the 3 buttons, each button represents a color. I have created the second activity and accepted value of the button clicked. I used a if loop to match the value of button, if the value of button matches with a color, it should display a string "You have chose (Color)". However, I am not sure about the parameters I have passed via intent object. can anyone help? and yeah, I am a noob, just started out to learn Android.

package org.example.helloandroid;

import android.support.v7.app.ActionBarActivity;

import android.support.v4.app.Fragment;

import android.content.Intent;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

public class MainActivity extends ActionBarActivity {

String EXTRA_MESSAGE= null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

/** if (savedInstanceState == null) {

getSupportFragmentManager().beginTransaction()

.add(R.id.container, new PlaceholderFragment())

.commit();

}

**/

}

@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 gotoActivity(View v) {

Button b= (Button)v;

String message= b.getText().toString();

if (message=="red") {

Intent intent = new Intent(this, ColorActivity.class);

intent.putExtra(EXTRA_MESSAGE, "You have chose RED ");

startActivity(intent);

}

if (message=="green") {

Intent intent= new Intent(this, ColorActivity.class);

intent.putExtra(EXTRA_MESSAGE, "You have chose GREEN!");

startActivity(intent);

}

else {

Intent intent= new Intent (this, ColorActivity.class);

intent.putExtra(EXTRA_MESSAGE, "You have chose BLUE");

startActivity(intent);

}

}

}

Here is the code for second activity- ColorActivity

package org.example.helloandroid;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.widget.TextView;

public class ColorActivity extends Activity {

String EXTRA_MESSAGE= "message";

TextView mTextview;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.color_activity);

mTextview=(TextView) findViewById(R.id.textView1);

mTextview.setText(getIntent().getStringExtra("EXTRA_MESSAGE"));

}

}

网友答案:

Make your second activity like this: Make sure that you set your String value EXTRA_MESSAGE to some value

    public class SecondActivity extends Activity {

    TextView mTextview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);
        mTextview = (TextView)findViewById(R.id.textView1);
        mTextview.setText(getIntent().getStringExtra("EXTRA_MESSAGE"));
    }
  }
网友答案:

Firstly, you haven't assigned EXTRA_MESSAGE. Change this line in MainActivity...

String EXTRA_MESSAGE= null;

...to...

String EXTRA_MESSAGE= "message";

Also, add the exact same line to your second Activity.

Second, you can't compare strings using == you have to use equals(...) method. Example...

if (message=="red")

...should be...

if (message.equals("red"))

Other than that just follow the format described by AndroidWarrior to get the extra from the Intent used to start your second Activity.

分享给朋友:
您可能感兴趣的文章:
随机阅读: