我想在两个活动之间传递信息。
我希望第一页上的分数出现在第二页的屏幕上。
我已经工作了2天但我不能。我认为这是因为这个原因。我没有用textView试过它。我还没有添加Intent和putExtra。我很困惑在哪里放短码。编写此代码的人可以添加代码并为我添加答案吗?
package com.taylansular.bluebird;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
public final static String EXTRA_MESSAGE = "skor";
TextView scoreText;
TextView timeText;
ImageView imageView1;
ImageView imageView2;
ImageView imageView3;
ImageView imageView4;
ImageView imageView5;
ImageView imageView6;
ImageView imageView7;
ImageView imageView8;
ImageView imageView9;
ImageView imageView10;
ImageView imageView11;
ImageView imageView12;
ImageView imageView13;
ImageView imageView14;
ImageView imageView15;
ImageView imageView16;
int score;
ImageView [] imageArray;
Handler handler;
Runnable runnable;
Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView) findViewById(R.id.imageView);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView3 = (ImageView) findViewById(R.id.imageView3);
imageView4 = (ImageView) findViewById(R.id.imageView4);
imageView5 = (ImageView) findViewById(R.id.imageView5);
imageView6 = (ImageView) findViewById(R.id.imageView6);
imageView7 = (ImageView) findViewById(R.id.imageView7);
imageView8 = (ImageView) findViewById(R.id.imageView8);
imageView9 = (ImageView) findViewById(R.id.imageView9);
imageView10 = (ImageView) findViewById(R.id.imageView10);
imageView11= (ImageView) findViewById(R.id.imageView11);
imageView12= (ImageView) findViewById(R.id.imageView12);
imageView13= (ImageView) findViewById(R.id.imageView13);
imageView14 = (ImageView) findViewById(R.id.imageView14);
imageView15= (ImageView) findViewById(R.id.imageView15);
imageView16= (ImageView) findViewById(R.id.imageView16);
imageArray = new ImageView[]{imageView1, imageView2,imageView3, imageView4,imageView5, imageView6,imageView7, imageView8,imageView9, imageView10,imageView11, imageView12,imageView13, imageView14,imageView15, imageView16};
hideImages();
score = 0;
CountDownTimer start = new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timeText = (TextView) findViewById(R.id.textTime);
timeText.setText("Time : " + millisUntilFinished / 1000);
}
@Override
public void onFinish() {
timeText = (TextView) findViewById(R.id.textTime);
timeText.setText("Time is up ! ");
handler.removeCallbacks(runnable);
for (ImageView image : imageArray) {
image.setVisibility(View.INVISIBLE);
}
int x = 0;
if (score < 10){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
MainActivity.this.startActivity(intent);
}
else if (score > 10){
Intent intent = new Intent(MainActivity.this, Main3Activity.class);
MainActivity.this.startActivity(intent);
}
}
}.start();
}
public void skoruArttır(View view){
scoreText = (TextView) findViewById(R.id.textScore);
score++;
scoreText.setText("Score " + score);
}
public void hideImages(){
handler = new Handler();
runnable = new Runnable(){
@Override
public void run() {
for (ImageView image : imageArray){
image.setVisibility(View.INVISIBLE);
}
Random r = new Random();
int i = r.nextInt(15-0);
imageArray[i].setVisibility(View.VISIBLE);
handler.postDelayed(this, 1000);
}
};
handler.post(runnable);
}
}
package com.taylansular.bluebird;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main2Activity extends AppCompatActivity {
TextView textView;
int score;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
TextView textView = (TextView) findViewById(R.id.seninskorun);
Button yenidenbasla = (Button) findViewById(R.id.yenidenbasla);
yenidenbasla.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View view) {
Intent ıntent = new Intent(Main2Activity.this, MainActivity.class);
startActivity(ıntent);
}
}
);
}
}
答案 0 :(得分:0)
假设得分是您要传递的,请在第一个活动中更改这些:
if (score < 10){
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("score", score);
MainActivity.this.startActivity(intent);
}
else if (score > 10){
Intent intent = new Intent(MainActivity.this, Main3Activity.class);
intent.putExtra("score", score);
MainActivity.this.startActivity(intent);
}
在第二个活动中,请按照以下方式进行:
setContentView(R.layout.activity_main2);
int score = getIntent.getIntExtra("score", 0);
TextView textView = (TextView) findViewById(R.id.seninskorun);
现在使用可以在第二个活动中的任何位置使用得分。这就是你传递价值的方法。