我曾嘗試通過Intent傳遞變量。但似乎我錯過了一些我不能傳遞整數變量時間的東西。這個問題已經被其他人問過了,但是我不能在這種情況下使用它。如何在android中通過intent傳遞整數變量?
我想通過intent將整型變量time的值傳遞給另一個aftertap.class類。
我有這段代碼。
public class ingame extends Activity {
private TextView textTimer;
int time = 0;
Timer t;
TimerTask task;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ingame);
Button button = (Button)findViewById(R.id.my_button);
final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
params.leftMargin = button.getWidth()+ new Random().nextInt(displaymetrics.widthPixels - 2*button.getWidth());
params.topMargin = button.getHeight()+ new Random().nextInt(displaymetrics.heightPixels - 3*button.getHeight());
Log.v("widthPixels", ""+ displaymetrics.widthPixels);
Log.v("heightPixels", ""+ displaymetrics.heightPixels);
button.setLayoutParams(params);
startTimer();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
t.cancel();
t.purge();
//Starting a new Intent
Intent thirdscreen = new Intent(getApplicationContext(), aftertap.class);
//Sending data to another Activity
thirdscreen.putExtra("time", time);
startActivity(thirdscreen);
}
});
}
public void startTimer(){
t = new Timer();
task = new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
textTimer = (TextView) findViewById(R.id.textTimer);
textTimer.setText(time + "");
time = time + 1;
}
});
}
};
t.scheduleAtFixedRate(task, 0, 1);
}
}
Aftertap類:
public class aftertap extends Activity{
TextView score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.aftertap);
TextView gameover = (TextView)findViewById(R.id.gameover);
score = (TextView)findViewById(R.id.score);
Intent i = getIntent();
// Receiving the Data
//I do not know how to get the value of the integer variable time. I want to display the value of time in the TextView score
}
}
你做了什麼錯誤? – Kedarnath