ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-从一个活动向另一个活动发送多个意图

2019-11-22 23:11:14  阅读:212  来源: 互联网

标签:android-studio android-intent java android


我对android非常陌生,我正在尝试将用户输入的数据(他们的名字)发送到另一个活动.过去,我能够使用Intent在活动之间发送单行代码,但是我无法解决如何向两个不同的TextView发送两个不同的字符串.

这是到目前为止我的MainActivity代码:

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);
}
}

我第二项活动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);
}

}

当我运行它时,我得到了两个TextView中都为“ name2”添加的内容.我需要做些什么来改变这个?

解决方法:

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

这是因为您正在使用第二个Intent创建Activity的新实例.您可以通过不同的方式来做到这一点.一个方法是创建一个Intent作为成员变量,在您的第一个函数调用中实例化它,添加额外功能,然后在第二个方法中添加另一个额外功能,然后在此处调用startActivity.

但是同时进行所有操作可能会更容易且更具可读性.

 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", player1Name);
    EditText player2 = (EditText) findViewById(R.id.player2);
    String player2Name= player2.getText().toString();
    intent2.putExtra("player2Name", player2Name);
    startActivity(intent);

只需调用此方法即可.

然后用

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 ;

标签:android-studio,android-intent,java,android
来源: https://codeday.me/bug/20191122/2063456.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有