从单个活动发送多个意图为另一个活动 [英] Sending Multiple Intents from a Single Activity to Another Activity

查看:130
本文介绍了从单个活动发送多个意图为另一个活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的Andr​​oid和我想用户输入的数据(自己的名字)发送到另一个活动。我在能够使用意图的活动之间发送单行过去,但我一直没能制定出如何发送两个不同的字符串到两个不同的TextViews。

下面是我的code为在MainActivity至今:

 包com.example.game;进口android.content.Intent;
进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.widget.Button;
进口android.widget.EditText;
进口android.view.View;
进口android.widget.AutoCompleteTextView;公共类MainActivity延伸活动{
公共最后静态字符串EXTRA_MESSAGE =com.example.myfirstapp.MESSAGE;
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);
    Button按钮=(按钮)findViewById(R.id.button);
    button.setOnClickListener(新View.OnClickListener(){
        @覆盖
        公共无效的onClick(查看视图){
            sendNames();
            sendNames2();
        }
    });
}
@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}
公共无效sendNames(){
    //发送PLAYER1的名字mainGame
    意向意图=新意图(这一点,MainGame.class);
    的EditText PLAYER1 =(EditText上)findViewById(R.id.player1);
    字符串消息= player1.getText()的toString()。
    intent.putExtra(EXTRA_MESSAGE,消息);
    startActivity(意向);
} //发送player2的名字mainGame
 公共无效sendNames2(){
    意图int​​ent2 =新意图(这一点,MainGame.class);
    的EditText player2 =(EditText上)findViewById(R.id.player2);
    。字符串消息2 = player2.getText()的toString();
    intent2.putExtra(EXTRA_MESSAGE,消息2);
    startActivity(intent2);
}
}

code为我的第二个活动,MainGame:

 包com.example.game;进口android.content.Intent;
进口android.os.Bundle;
进口android.app.Activity;
进口android.view.Menu;
进口android.annotation.Sup pressLint;
进口android.view.MenuItem;
进口android.support.v4.app.NavUtils;
进口android.annotation.TargetApi;
进口android.os.Build;
进口android.widget.TextView;公共类MainGame延伸活动{
@燮pressLint(NewAPI)
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_game);
    // retrives PLAYER1的名字
    意向意图= getIntent();
    字符串消息= intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView的名称1 =(的TextView)findViewById(R.id.name1);
    name1.setText(消息);
    // retrivews player2的名字
    意向intent2 = getIntent();
    字符串消息2 = intent2.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView的名称2 =(的TextView)findViewById(R.id.name2);
    name2.setText(消息2);    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
        getActionBar()setDisplayHomeAsUpEnabled(真)。
    }}       返回super.onOptionsItemSelected(项目);
}}

当我运行它,我得到什么已经投入了两TextViewsNAME2。什么我需要做的改变呢?


解决方案

  

当我运行此我得到什么已经投入在'名2在这两个TextView中的


这是因为你正在创建的活动与第二意图的新实例。有不同的方式,你可以做到这一点。其中之一是建立一个单一的意图作为成员变量,实例化它在你的第一个函数调用,增加群众演员,然后添加其他额外的第二种方法,并调用 startActivity 那里。

但它可能会更容易,更具可读性,只是做这一切在同一时间。

 公共无效sendNames(){
    //发送PLAYER1的名字mainGame
    意向意图=新意图(这一点,MainGame.class);
    的EditText PLAYER1 =(EditText上)findViewById(R.id.player1);
    字符串player1Name = player1.getText()的toString()。
    intent.putExtra(player1Name,邮件);
    的EditText player2 =(EditText上)findViewById(R.id.player2);
    字符串player2Name = player2.getText()的toString()。
    intent2.putExtra(player2Name,player2Name);
    startActivity(意向);

和只是调用这个方法之一。

然后用得到它。

 意向意图= getIntent();
串名1 = intent.getStringExtra(player1Name);
TextView的名称1 =(的TextView)findViewById(R.id.name1);
name1.setText(消息);串名1 = intent2.getStringExtra(player2Name);
TextView的名称2 =(的TextView)findViewById(R.id.name2);
name2.setText(名称1;

I am very new to android, and I am trying to send user-inputted data (their names) to another activity. I have in the past been able to send single lines between activities using Intents, but I have not been able to work out how to send two different strings to two different TextViews.

Here is my code for the MainActivity so far:

package com.example.game;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import android.widget.AutoCompleteTextView;

public class MainActivity extends Activity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button = (Button)findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            sendNames();
            sendNames2();
        }
    });
}


@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 sendNames() {
    //sends player1's name to mainGame
    Intent intent = new Intent (this, MainGame.class);
    EditText player1 = (EditText) findViewById(R.id.player1);
    String message = player1.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
}//sends player2's name to mainGame
 public void sendNames2(){
    Intent intent2 = new Intent(this, MainGame.class);
    EditText player2 = (EditText) findViewById(R.id.player2);
    String message2 = player2.getText().toString();
    intent2.putExtra(EXTRA_MESSAGE, message2);
    startActivity(intent2);
}
}

Code for my second activity, MainGame:

package com.example.game;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.annotation.SuppressLint;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import android.annotation.TargetApi;
import android.os.Build;
import android.widget.TextView;

public class MainGame extends Activity {
@SuppressLint("NewAPI")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    //retrives player1's name
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView name1 = (TextView) findViewById(R.id.name1);
    name1.setText(message);
    //retrivews player2's name
    Intent intent2 = getIntent();
    String message2 = intent2.getStringExtra(MainActivity.EXTRA_MESSAGE);
    TextView name2 = (TextView) findViewById(R.id.name2);
    name2.setText(message2);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

       return super.onOptionsItemSelected(item);
}

}

When I run this, I get whatever has been put in for 'name2' in both TextViews. What do I need to do to change this?

解决方案

when i run this i get whatever has been put in for 'name2' in both TextView's

This is because you are creating a new instance of the Activity with the second Intent. There are different ways you could do it. One would be create a single Intent as a member variable, instantiate it in your first function call, add extras, then add the other extra in the second method, and call startActivity there.

But it would probably be easier and more readable to just do it all at the same time.

 public void sendNames() {
    //sends player1's name to mainGame
    Intent intent = new Intent (this, MainGame.class);
    EditText player1 = (EditText) findViewById(R.id.player1);
    String player1Name= player1.getText().toString();
    intent.putExtra("player1Name", message);
    EditText player2 = (EditText) findViewById(R.id.player2);
    String player2Name= player2.getText().toString();
    intent2.putExtra("player2Name", player2Name);
    startActivity(intent);

And just call this one method.

Then get it with

Intent intent = getIntent();
String name1 = intent.getStringExtra("player1Name");
TextView name1 = (TextView) findViewById(R.id.name1);
name1.setText(message);

String name1 = intent2.getStringExtra("player2Name");
TextView name2 = (TextView) findViewById(R.id.name2);
name2.setText(name1 ;

这篇关于从单个活动发送多个意图为另一个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆