#布局文件
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackgrounnd"
tools:context=".MainActivity">
<TextView
android:id="@+id/game_score_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/your_score"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />
<TextView
android:id="@+id/time_left_text_view"
android:layout_width="97dp"
android:layout_height="wrap_content"
android:layout_marginStart="325dp"
android:text="@string/time_left"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.022" />
<Button
android:id="@+id/tap_me_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/tap_me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
#string.xml
<resources>
<string name="app_name">TimeFighter2</string>
<string name="tap_me">Tap me!</string>
<string name="your_score">Your Score: </string>
<string name="time_left">Time left: </string>
<string name="game_over_message">Tap up! Your Score was: %1$d</string>
<string name="about_title">Timefighter </string>
<string name="about_message">Create by LinLinLin</string>
</resources>
#核心代码
mport androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Dialog;
import android.icu.text.UFormat;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button mybutton;
private TextView gamescore,timeleft;
private int count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mybutton = findViewById(R.id.tap_me_button);
gamescore = findViewById(R.id.game_score_text_view);
timeleft =findViewById(R.id.time_left_text_view);
mybutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
count++;
gamescore.setText("Your Score:"+count+"");
}
});
CountDownTimer timer = new CountDownTimer(60*1000,1000) {/*倒计时 参数一倒计时的总时间 参数二 倒计时的间隔*/
@Override
public void onTick(long millisUntilFinished) {
timeleft.setText("还剩"+millisUntilFinished/1000+"秒");
}
@Override
public void onFinish() {
timeleft.setText("时间到");
gamescore.setText("共点击了"+count+"次");
mybutton.setEnabled(false);
}
}.start();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,menu);//创建选项菜单
return true;
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.about_item:
new AlertDialog.Builder(MainActivity.this)
.setTitle("TimeFighter1.0")
.setMessage("create Yin Yi Qiang")
.show();
}
return super.onOptionsItemSelected(item);
}
}