Layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:gravity="center" android:background="#00BFFF" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Level" android:textSize="30dp" android:textColor="#ffffff"/> <TextView android:id="@+id/level_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:textSize="30dp" android:layout_marginLeft="10dp" android:text="1"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_margin="10dp" android:orientation="horizontal"> <TextView android:id="@+id/txt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0" android:layout_margin="10dp" android:textColor="#ffffff" android:textSize="20dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="/" android:layout_margin="10dp" android:textSize="20dp" android:textColor="#ffffff"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#ffffff" android:layout_margin="10dp" android:textSize="20dp" android:text="10"/> </LinearLayout> <androidx.cardview.widget.CardView android:layout_width="wrap_content" app:cardCornerRadius="10dp" android:layout_height="wrap_content"> <ProgressBar android:id="@+id/progressBar" android:layout_width="200dp" android:layout_height="10dp" android:max="100" android:min="10" android:secondaryProgress="70" style="@style/Widget.AppCompat.ProgressBar.Horizontal" android:layout_marginTop="10dp"/> </androidx.cardview.widget.CardView> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/start" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#000000" android:textSize="15dp" android:layout_margin="10dp" android:text="开始"/> </LinearLayout> </LinearLayout>
MainActivity
package com.example.userrecyclerviewapplication; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ProgressBar; import android.widget.TextView; public class MainActivity extends AppCompatActivity { private TextView txt,level_num; private ProgressBar pb; private Button start; private int level = 1 ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); level_num = findViewById(R.id.level_num); txt = findViewById(R.id.txt); pb = findViewById(R.id.progressBar); start = findViewById(R.id.start); pb.getProgressDrawable().setColorFilter(Color.YELLOW,android.graphics.PorterDuff.Mode.SRC_IN); initEvent(); } private void initEvent() { start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int progress = pb.getProgress(); int real_num = progress/10; if(real_num <10){ progress = progress+10; txt.setText(String.valueOf(real_num)); pb.setProgress(progress); }else { pb.setProgress(0); txt.setText("0"); level++; level_num.setText(String.valueOf(level)); } } }); } }