android数据使用 Intent 传递给另一个 Activity

标签 android

你好,我是 Android 编程新手。我想问一下如何使用 Intent 将数据传递给另一个 Activity ?我的情况是,我有 3 个单选按钮,如果用户单击第一个按钮并最终单击“确定”按钮,则应该将其检索(文本)到其他 Activity 。

0 选项 1 0 选项 2 0 选项 3

然后在其他 Activity 中应该是这样的: 选择选项 1。

最佳答案

您可以在两个 Activity 之间传递数据:

calculate = (Button) findViewById(R.id.calculateButton);
private OnClickListener calculateButtonListener = new OnClickListener() {

        @Override
        public void onClick(View arg0) {
        String strtext="";
             if(RadioButton1.isChecked())
             {
              strtext=RadioButton1.getText();
             }
             if(RadioButton2.isChecked())
             {
             strtext=RadioButton1.getText();
             }
             if(RadioButton1.isChecked())
             {
              strtext=RadioButton3.getText();
             }
             if(!strtext.equals(""))
             {
               //Create new Intent Object, and specify class
                Intent intent = new Intent();  
                intent.setClass(SenderActivity.this,Receiveractivity.class);

                //Set your data using putExtra method which take 
                //any key and value which we want to send 
                intent.putExtra("senddata",strtext);  

                //Use startActivity or startActivityForResult for Starting New Activity
                SenderActivity.this.startActivity(intent); 
             }
        }
    };

在接收 Activity 中:

//obtain  Intent Object send  from SenderActivity
  Intent intent = this.getIntent();

  /* Obtain String from Intent  */
  if(intent !=null)
  {
     String strdata = intent.getExtras().getString("senddata");
    // DO SOMETHING HERE
  }
  else
  {
    // DO SOMETHING HERE
  }

关于android数据使用 Intent 传递给另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11437081/

相关文章:

android - 在 fragment 和 Activity 之间更新 ViewModel

android - 有没有办法在 PendingIntent 被触发时终止 Activity ?

Android Room - 简单的选择查询 - 无法访问主线程上的数据库

java - Android javac和dx——麻烦处理,类名和路径不匹配

android - 如何在 Android 上使用 JNI 训练 tensorflow 网络?

android - 在屏幕方向更改android上调整视频 View

android - 从没有开启Root的设备中的 Assets 文件夹复制数据库

android - 从日历打开我的应用程序

android - 丢失了我的数据...能够以某种方式从我的 APK 获取我的应用程序源吗?

android - Textview 排在前面 Imageview