4个activity从左到右,第一个activi ty是一个进度条,走满了就跳转到第二个activity。第2个activit是用sp存储模拟登入界面,满足情况跳转到第3个activity 第3.个activity有一个开始按钮,和一个2分球按钮和3分球按钮 开始按钮按了开始倒计时10秒钟,然后2分球按钮按了有百分之70的概率得2分,3分球按钮有百分之40概率得三分,总分显示出来 当倒计时结束跳转到第4个activity显示总分
这是一个简单的Android应用程序,其中包含4个Activity。第一个Activity是一个进度条,当进度条走满时,会跳转到第二个Activity。第二个Activity是一个模拟登录界面,使用SharedPreferences存储用户信息。如果用户成功登录,将跳转到第三个Activity。第三个Activity有一个开始按钮和两个投篮按钮,按下开始按钮后,将开始一个10秒的倒计时。按下投篮按钮后,有一定的概率得分,得分将累加到总分中。当倒计时结束时,将跳转到第四个Activity,显示总分。
以下是一个简单的实现示例:
第一个Activity的布局文件:
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:max="100"
android:progress="0" />
第一个Activity的代码:
public class FirstActivity extends AppCompatActivity {
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
progressBar = findViewById(R.id.progressBar);
// 模拟进度条的进度增加
new Thread(new Runnable() {
@Override
public void run() {
for (int i = 0; i <= 100; i++) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
final int progress = i;
runOnUiThread(new Runnable() {
@Override
public void run() {
progressBar.setProgress(progress);
if (progress == 100) {
// 进度条走满后跳转到第二个Activity
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
finish();
}
}
});
}
}
}).start();
}
}
第二个Activity的布局文件:
<EditText
android:id="@+id/usernameEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Username" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/loginButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Login" />
第二个Activity的代码:
public class SecondActivity extends AppCompatActivity {
private EditText usernameEditText;
private EditText passwordEditText;
private Button loginButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
usernameEditText = findViewById(R.id.usernameEditText);
passwordEditText = findViewById(R.id.passwordEditText);
loginButton = findViewById(R.id.loginButton);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 模拟登录
String username = usernameEditText.getText().toString();
String password = passwordEditText.getText().toString();
if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
Toast.makeText(SecondActivity.this, "Please enter username and password", Toast.LENGTH_SHORT).show();
} else {
// 保存用户信息到SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("user", MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("username", username);
editor.putString("password", password);
editor.apply();
// 登录成功后跳转到第三个Activity
Intent intent = new Intent(SecondActivity.this, ThirdActivity.class);
startActivity(intent);
finish();
}
}
});
}
}
第三个Activity的布局文件:
<TextView
android:id="@+id/timerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="10" />
<Button
android:id="@+id/startButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Start" />
<Button
android:id="@+id/twoPointsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="2 Points" />
<Button
android:id="@+id/threePointsButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="3 Points" />
<TextView
android:id="@+id/scoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Score: 0" />
第三个Activity的代码:
public class ThirdActivity extends AppCompatActivity {
private TextView timerTextView;
private Button startButton;
private Button twoPointsButton;
private Button threePointsButton;
private TextView scoreTextView;
private int score = 0;
private boolean isStarted = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
timerTextView = findViewById(R.id.timerTextView);
startButton = findViewById(R.id.startButton);
twoPointsButton = findViewById(R.id.twoPointsButton);
threePointsButton = findViewById(R.id.threePointsButton);
scoreTextView = findViewById(R.id.scoreTextView);
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!isStarted) {
// 开始倒计时
new CountDownTimer(10000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
timerTextView.setText(String.valueOf(millisUntilFinished / 1000));
}
@Override
public void onFinish() {
// 倒计时结束后跳转到第四个Activity
Intent intent = new Intent(ThirdActivity.this, FourthActivity.class);
intent.putExtra("score", score);
startActivity(intent);
finish();
}
}.start();
isStarted = true;
}
}
});
twoPointsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 70%的概率得2分
if (Math.random() < 0.7) {
score += 2;
scoreTextView.setText("Score: " + score);
}
}
});
threePointsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 40%的概率得3分
if (Math.random() < 0.4) {
score += 3;
scoreTextView.setText("Score: " + score);
}
}
});
}
}
第四个Activity的布局文件:
<TextView
android:id="@+id/scoreTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="Score: 0" />
第四个Activity的代码:
public class FourthActivity extends AppCompatActivity {
private TextView scoreTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fourth);
scoreTextView = findViewById(R.id.scoreTextView);
// 显示总分
int score = getIntent().getIntExtra("score", 0);
scoreTextView.setText("Score: " + score);
}
}