将一个Kotlin活动的预计算结果发送到另一个Kotlin活动中的textview

时间:2017-08-28 20:03:52

标签: android android-intent android-activity kotlin textview

我目前正在主要活动中进行计算。计算在下面的函数中完成。结果显示在同一屏幕上的文本框中。

ShowNumber.setText(buClickValue)

 Bill = buClickValue

  fun buCalculateEvent(view: View) {
       var Total = "%.2f".format(Bill.toDouble() * .15)

      ShowNumber.setText("Based on a 15% tip, you would be " + ( Bill.toDouble() + Total.toDouble()))
     }

我的问题是尝试将setText消息显示在另一个活动中。我知道如何通过点击按钮访问活动。我也知道如何在另一个活动中生成Toast消息。如何将信息发送到另一个活动的文本视图?

先谢谢。

2 个答案:

答案 0 :(得分:2)

我不是Kotlin的开发人员,但我认为它确实通过startActivity启动Activity,在启动之前,您可以使用setIntegerExtra(A_KEY,value)(或任何其他额外的)将您的值添加到intent中在你接受的第二项活动中:

//app.component.ts:
import { Component } from '@angular/core';

@Component({
  selector: 'my-app',  
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']  
})

export class AppComponent {
  title = 'Tour of Heroes';  
}

//app.component.html
<!--The content below is only a placeholder and can be replaced.-->
<div>
    Welcome to {{title}}!
    <my-heroes></my-heroes>
</div>

//app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { AppComponent } from './app.component';
import { HeroDetailComponent } from './hero-detail.component';
import { HeroesComponent } from './heroes.component';
import { HeroService } from './hero.service';

@NgModule({
  imports: [
    BrowserModule,
    FormsModule
  ],
  declarations: [
    AppComponent,
    HeroDetailComponent,
    HeroesComponent
  ],
  providers: [HeroService],
  bootstrap: [AppComponent]
})
export class AppModule { }

//hero-detail.component.ts
import { Component, Input } from '@angular/core';
import { Hero } from './hero';

@Component({
   selector: 'hero-detail',
   templateUrl: 'hero-detail.component.html'
})
export class HeroDetailComponent {
    @Input() hero: Hero;
}

//hero-detail.component.html
<!--The content below is only a placeholder and can be replaced.-->
<div>
    <div *ngIf="hero">
        <div>{{hero.name}} details</div>
        <div><label>id:</label>{{hero.id}}</div>
        <div><label>name:</label><input [(ngModel)]="hero.name" placeholder="name" /></div>
    </div>    
</div>

//hero.service.ts
import { Injectable } from '@angular/core';
import {Hero} from './hero';
import {HEROES} from './mock-heroes';

@Injectable()
export class HeroService { 
  getHeroes(): Promise<Hero[]> {
     return Promise.resolve(HEROES);
  }

  getHeroesSlowly(): Promise<Hero[]> {
    return new Promise(resolve=> {
      setTimeout(()=>resolve(this.getHeroes()), 2000);
    });
  }
}

//hero.ts
export class Hero {
 id: number;
 name: string;
 }

//heroes.component.html
import { Component, OnInit } from '@angular/core';
import { Hero } from './hero';
import { HeroService } from './hero.service';

@Component({
  selector: 'my-heroes',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']  
})

export class HeroesComponent implements OnInit {
  title = 'Tour of Heroes';
  heroes: Hero[];  
  selectedHero: Hero;

  constructor(private heroService: HeroService) {}

  onSelect(hero: Hero): void{
    this.selectedHero = hero;
  }

  getHeroes(): void {
    this.heroService.getHeroesSlowly().then(heroes => this.heroes = heroes);
  }

  ngOnInit(): void {
    this.getHeroes();
  }
}

//mock-heroes.ts
import {Hero} from './hero';

export const HEROES: Hero[] = [
    {id: 11, name: 'Mr. Nice'},
    {id: 12, name: 'Narco'},
    {id: 13, name: 'Bombasto'},
    {id: 14, name: 'Celeritas'},
    {id: 15, name: 'Magneta'},
    {id: 16, name: 'RubberMan'},
    {id: 17, name: 'Dynama'},
    {id: 18, name: 'Dr IQ'},
    {id: 19, name: 'Mr. Nice'},
    {id: 20, name: 'Magma'},
    {id: 21, name: 'Tornado'}   
  ];

//index.html
<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>My NG App</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
  <my-root></my-root>
</body>
</html>

//main.ts
import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);

答案 1 :(得分:1)

我想翻译马科斯&#39;在kotlin回答。

在开始第二个活动之前,将值放在意图中,如下所示;

val intent = Intent(this, SecondActivity::class.java)
intent.putExtra("total", theValueTotal)
startActivity(intent)

然后在您的第二个活动中,您可以使用此获取值;

val total = intent.getDoubleExtra("total", 0.0)