【问题标题】:Pass variable value to another TextView Activity将变量值传递给另一个 TextView Activity
【发布时间】:2016-08-09 13:29:46
【问题描述】:

作为一个Activity的一个变量的步长值对于一个TextView的其他Activity?可以帮忙吗?

谢谢!

((见图片说明))

【问题讨论】:

标签: java android


【解决方案1】:

最简单的方法是使用 Intent: in first Activity

Intent intent =new Intent(CurrentClass.this,DisClass.class);
intent.putExtra("myTextValue",textView.getText().toString());
startActivity(intent);

在 dist 活动中执行以下操作:

String myValue=getIntent().getExtra().getString("myTextValue");
textView.setText(myValue);

【讨论】:

    【解决方案2】:

    您可以使用本地广播接收器。

    Activity B 中的第一个注册接收者

      //in onCreate Method 
      LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver,
                        new IntentFilter("my-event-name"));
    
    
    // It will be called whenever an Intent
    // with an action named "my-event-name" is broadcasted.
    
     private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
     @Override
     public void onReceive(Context context, Intent intent) {
        // Get extra data included in the Intent
         String message = intent.getStringExtra("message");
       // show this message in textview 
     }
    };
    

    在活动 A 中

    //broadcast this 
    Intent intent = new Intent("my-event-name");
    intent.putExtra("message", Integer(sumSettlment).toString());
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    

    【讨论】:

      【解决方案3】:
      Intent activityTwo = new Intent(this, Activity2.class);
      activityTwo.putIntExtra("key", sumSettlement);
      startActivity(activityTwo);
      

      现在,Activity2:

      if(getIntent() != null) {
          textView.setText(String.valueOf(getIntent.getExtra("key"));
      }
      

      【讨论】:

        猜你喜欢
        • 2012-04-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-14
        • 1970-01-01
        相关资源
        最近更新 更多