Android 如何在其他活动中发送值?

Android 如何在其他活动中发送值?,android,android-intent,kotlin,Android,Android Intent,Kotlin,你好,对不起我的英语,我是法国人。我正在学习Android开发,并尝试在其他活动中发送int值。我已经将一个int变量声明为0,当我按下一个按钮时,该变量在每个按钮的其他值上变为1。我知道如何创建一个意图,但我如何才能让它得到我的按钮的价值。谢谢。第一个活动 Intent intent =new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("value", yourValue);

你好,对不起我的英语,我是法国人。我正在学习Android开发,并尝试在其他活动中发送int值。我已经将一个int变量声明为0,当我按下一个按钮时,该变量在每个按钮的其他值上变为1。我知道如何创建一个意图,但我如何才能让它得到我的按钮的价值。谢谢。

第一个活动

        Intent intent =new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("value", yourValue);
        startActivity(intent);
第二项活动

public class SecondActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.intent);

        Intent iin = getIntent();
        Bundle bundle = iin.getExtras();

        if(bundle != null)
        {
            String name = (String) bundle.get("name");

        }
    }
}
第一项活动

        Intent intent =new Intent(MainActivity.this, SecondActivity.class);
        intent.putExtra("value", yourValue);
        startActivity(intent);
第二项活动

public class SecondActivity extends Activity
{

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.intent);

        Intent iin = getIntent();
        Bundle bundle = iin.getExtras();

        if(bundle != null)
        {
            String name = (String) bundle.get("name");

        }
    }
}
很简单

在发送端

int intValue
=从编辑文本或按钮获取值

使用
Intent.putExtra
设置值

Intent myIntent = new Intent(test1.this, test2.class);
myIntent.putExtra("yourname", intValue);
startActivity(myIntent);
在接收器端

使用
Intent.getIntExtra
获取值

Intent mIntent = getIntent();
 int intValue = mIntent.getIntExtra("yourname", 0);
intValue
是您的值

很简单

在发送端

int intValue
=从编辑文本或按钮获取值

使用
Intent.putExtra
设置值

Intent myIntent = new Intent(test1.this, test2.class);
myIntent.putExtra("yourname", intValue);
startActivity(myIntent);
在接收器端

使用
Intent.getIntExtra
获取值

Intent mIntent = getIntent();
 int intValue = mIntent.getIntExtra("yourname", 0);

intValue
是您的值

为目标活动创建意图,然后使用方法将数据放入意图中

  val intent = Intent(context, TargetActivity::class.java)
  intent.putExtra("some_value", value)
然后开始你的活动

startActivity(intent)

为目标活动创建意图,然后使用方法将数据放入意图中

  val intent = Intent(context, TargetActivity::class.java)
  intent.putExtra("some_value", value)
然后开始你的活动

startActivity(intent)

您需要在意图中使用putExtra来添加要发送到下一个活动的int值,如下所示:

val intent = Intent(this, NextActivity::class.java)
intent.putExtra("samplevalue", 1)
startActivity(intent)
然后在该活动(NextActivity)上,您将使用下面的代码检索该值

val buttonValue:Int = intent.getIntExtra("samplevalue", 0)

您需要在意图中使用putExtra来添加要发送到下一个活动的int值,如下所示:

val intent = Intent(this, NextActivity::class.java)
intent.putExtra("samplevalue", 1)
startActivity(intent)
然后在该活动(NextActivity)上,您将使用下面的代码检索该值

val buttonValue:Int = intent.getIntExtra("samplevalue", 0)

您可以通过将键值对放入intent bundle中来指定参数:

// ActivityOne.java
public void launch() {
  // first parameter is the context, second is the class of the activity to launch
  Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
  // put "extras" into the bundle for access in the second activity
  i.putExtra("username", "foobar"); 
  i.putExtra("in_reply_to", "george"); 
  i.putExtra("code", 400);
  // brings up the second activity
  startActivity(i); 
}
将数据添加到捆绑包中后,您可以轻松访问已启动活动中的数据:

// ActivityTwo.java (subactivity) can access any extras passed in
protected void onCreate(Bundle savedInstanceState) {
   String username = getIntent().getStringExtra("username");
   String inReplyTo = getIntent().getStringExtra("in_reply_to");
   int code = getIntent().getIntExtra("code", 0);
}

更多信息检查

您可以通过将键值对放入intent bundle中来指定参数:

// ActivityOne.java
public void launch() {
  // first parameter is the context, second is the class of the activity to launch
  Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
  // put "extras" into the bundle for access in the second activity
  i.putExtra("username", "foobar"); 
  i.putExtra("in_reply_to", "george"); 
  i.putExtra("code", 400);
  // brings up the second activity
  startActivity(i); 
}
将数据添加到捆绑包中后,您可以轻松访问已启动活动中的数据:

// ActivityTwo.java (subactivity) can access any extras passed in
protected void onCreate(Bundle savedInstanceState) {
   String username = getIntent().getStringExtra("username");
   String inReplyTo = getIntent().getStringExtra("in_reply_to");
   int code = getIntent().getIntExtra("code", 0);
}
更多信息检查

检查此答案:可能重复检查此答案:可能重复