我正在开发应用程序,它将在textview中显示分数,然后在另一项活动中获得该分数。我使用以下代码执行此操作:
#include <thread>
#include <mutex>
#include <iostream>
#include <chrono>
#include <atomic>
using namespace std; // Kids, don't try this at home.
using stopper = atomic<bool>;
void function_( stopper *ptrStop) {
for (int i = 0; i < 10; i++) {
if (*ptrStop) {
break;
}
//doSomething();
this_thread::sleep_for(chrono::milliseconds(10));
cout << "Iteration " << i << endl;
//this_thread::sleep_for(chrono::milliseconds(1));
}
*ptrStop = false;
return;
}
int main() {
stopper stop{ false };
thread functionThread(function_, &stop);
this_thread::sleep_for(chrono::milliseconds(100));
stop = true;
// // The following is optional
///while (stop == true) {
// this_thread::yield();
//}
cout << "Changed boolean variable value" << endl;
functionThread.join();
return 0;
}
现在我需要在第二类本身的静态方法中使用已经保存在txtscore中的分数,其中需要添加5个点,总共10个。
我有一个静态方法:
//in the first activity
int score= 0;
score= score + 5;
txtscore.setText("Score: " + score);
Intent intent = new Intent(context, AnotherActivity.class);
intent.putExtra("Score",score+ " ");
context.startActivity(intent);
//in second activity (AnotherActivity.class)
Bundle bundle = getIntent().getExtras();
if (bundle != null){
int score= bundle.getInt("0");
txtscore.setText("0" + score);
}
任何帮助?
答案 0 :(得分:0)
只需使用:
getIntent().getExtras().getString("Score");
在你的第二个活动中
答案 1 :(得分:0)
不是将得分作为String传递,而是必须将其解析为int,最好将得分捆绑为int:
活动一
int score = 5;
Intent.putExtra("score", score);
活动二
int score = bundle.getIntExtra("score");
score += 5;
txtScore.setText("SCORE: " + score);