在活动中使用多个意图

时间:2015-08-02 14:47:00

标签: android android-intent

我有6页第1-5页按钮点击发送字符串到第6页,第6页使用意图在textviews中显示它们 但当我到达第6页时,它只显示所有textview中的最后一个字符串

第1页

public class page1 extends ActionBarActivity {


public static final String EXTRA_MESSAGE="";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page1);



}


public void cow (View view){
    startActivity(new Intent(this, page2.class));
    Intent intent = new Intent(this, resault.class);
    String message = "پیشرو بودن";
    intent.putExtra(EXTRA_MESSAGE, message);

}
public void horse (View view){

    startActivity(new Intent(this, page2.class));
    Intent intent = new Intent(this, resault.class);
    String message = "خانواده";
    intent.putExtra(EXTRA_MESSAGE, message);
}
public void pig (View view){

    startActivity(new Intent(this, page2.class));
    Intent intent = new Intent(this, resault.class);
    String message = "پول";
    intent.putExtra(EXTRA_MESSAGE, message);
}
public void tiger (View view){

    startActivity(new Intent(this, page2.class));
    Intent intent = new Intent(this, resault.class);
    String message = "غرور و افتخار";
    intent.putExtra(EXTRA_MESSAGE, message);
}
public void sheep (View view){

    startActivity(new Intent(this, page2.class));
    Intent intent = new Intent(this, resault.class);
    String message = "عشق";
    intent.putExtra(EXTRA_MESSAGE, message);
}

}

到第4页相同,但我更改了数字

第5页

public class page5 extends ActionBarActivity {

public static final String EXTRA_MESSAGE5="";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_page5);
}


public void cow (View view){
    Intent intent5 = new Intent(this, resault.class);
    String message5 = "پیشرو بودن";
    intent5.putExtra(EXTRA_MESSAGE5, message5);
    startActivity(intent5);
}

public void horse (View view){
    Intent intent5 = new Intent(this, resault.class);
    String message5 = "خانواده";
    intent5.putExtra(EXTRA_MESSAGE5, message5);
    startActivity(intent5);
}

public void pig (View view){
    Intent intent5 = new Intent(this, resault.class);
    String message5 = "پول";
    intent5.putExtra(EXTRA_MESSAGE5, message5);
    startActivity(intent5);
}

public void tiger (View view){
    Intent intent5 = new Intent(this, resault.class);
    String message5 = "غرور و افتخار";
    intent5.putExtra(EXTRA_MESSAGE5, message5);
    startActivity(intent5);
}

public void sheep (View view){


    Intent intent5 = new Intent(this, resault.class);
    String message5 = "عشق";
    intent5.putExtra(EXTRA_MESSAGE5, message5);
    startActivity(intent5);
}


}

接收者页面(第6页)

public class resault extends ActionBarActivity {


TextView ch1;
TextView ch2;
TextView ch3;
TextView ch4;
TextView ch5;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_resault);







    // choise1

    ch1 = (TextView) findViewById(R.id.choice1);
    Intent intent;
    intent = getIntent();
    String message = intent.getStringExtra(page1.EXTRA_MESSAGE);
    ch1.setText(message);

    // choise2

    ch2 = (TextView) findViewById(R.id.choice2);
    Intent intent2;
    intent2 = getIntent();
    String message2 = intent2.getStringExtra(page2.EXTRA_MESSAGE2);
    ch2.setText(message2);


    // choise3

    ch3 = (TextView) findViewById(R.id.choice3);
    Intent intent3;
    intent3 = getIntent();
    String message3 = intent3.getStringExtra(page3.EXTRA_MESSAGE3);
    ch3.setText(message3);

    // choise4

    ch4 = (TextView) findViewById(R.id.choice4);
    Intent intent4;
    intent4 = getIntent();
    String message4 = intent4.getStringExtra(page4.EXTRA_MESSAGE4);
    ch4.setText(message4);


    // choise5

    ch5 = (TextView) findViewById(R.id.choice5);
    Intent intent5;
    intent5 = getIntent();
    String message5 = intent5.getStringExtra(page5.EXTRA_MESSAGE5);
    ch5.setText(message5);


}




}

真的非常感谢你的帮助

1 个答案:

答案 0 :(得分:0)

您需要调整逻辑。例如,在第6页中:

public class resault extends ActionBarActivity {
    public final static String EXTRA_MASSAGE = "massage";
    public final static String EXTRA_PAGE = "page";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resault);
        String text = getIntent().getStringExtra(EXTRA_MASSAGE);
        if (text != null ) {
            int page = getIntent().getIntExtra(EXTRA_PAGE, -1);
            switch (page) {
                case 1:
                    ((TextView) findViewById(R.id.choice1)).setText(text);
                    break;
                case 2:
                    ((TextView) findViewById(R.id.choice2)).setText(text);
                    break;
                case 3:
                    ((TextView) findViewById(R.id.choice3)).setText(text);
                    break;
                case 4:
                    ((TextView) findViewById(R.id.choice4)).setText(text);
                    break;
                case 5:
                    ((TextView) findViewById(R.id.choice5)).setText(text);
                    break;

            }
        }

    }
}

然后你在第3页做了例如:

 Intent intent = new Intent(this, resault.class);
    String message = "خانواده";
    intent.putExtra(Result.EXTRA_PAGE, 3);
    intent.putExtra(Result.EXTRA_MASSAGE, message);
    startActivity(intent);