猿问

Android 百分比栏 onclicklistener

我是 Java 和编码方面的新手,我尝试学习如何在 Android Studio 中编码并尝试一些练习。

我想为 2 个按钮创建代码:bt1 和 bt2。当用户点击 bt1 时,进度条显示点击此 bt1 的百分比。

此解决方案的任何其他帮助/方法也将是可观的。


叮当猫咪
浏览 129回答 1
1回答

慕斯709654

因此,据我从你的描述和我们的谈话中了解到,你需要一个进度条来显示点击第一个和第二个按钮之间的百分比。如果您创建一个 Empty Activity 项目并替换以下文件中的所有这些内容:MainActivity.javapublic class MainActivity extends AppCompatActivity {Button btn1;Button btn2;ProgressBar myBar;TextView score1;TextView score2;TextView percent1;TextView percent2;private float firstClicked;private float secondClicked;@RequiresApi(api = Build.VERSION_CODES.N)@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; btn1 = findViewById(R.id.btn1);&nbsp; &nbsp; btn2 = findViewById(R.id.btn2);&nbsp; &nbsp; myBar = findViewById(R.id.progressBar);&nbsp; &nbsp; score1 = findViewById(R.id.score1);&nbsp; &nbsp; score2 = findViewById(R.id.score2);&nbsp; &nbsp; percent1 = findViewById(R.id.percent1);&nbsp; &nbsp; percent2 = findViewById(R.id.percent2);&nbsp; &nbsp; firstClicked = 0;&nbsp; &nbsp; secondClicked = 0;&nbsp; &nbsp; btn1.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @RequiresApi(api = Build.VERSION_CODES.N)&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; firstClicked++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score1.setText(String.valueOf((int) firstClicked));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateProgressTab();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; btn2.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @RequiresApi(api = Build.VERSION_CODES.N)&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; secondClicked++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; score2.setText(String.valueOf((int) secondClicked));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateProgressTab();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; updateProgressTab();}@SuppressLint("SetTextI18n")@RequiresApi(api = Build.VERSION_CODES.N)public void updateProgressTab() {&nbsp; &nbsp; float progress;&nbsp; &nbsp; if (firstClicked != 0 && secondClicked != 0) {&nbsp; &nbsp; &nbsp; &nbsp; progress = (firstClicked / (firstClicked + secondClicked)) * 100;&nbsp; &nbsp; &nbsp; &nbsp; percent1.setText((int) progress + "%");&nbsp; &nbsp; &nbsp; &nbsp; percent2.setText((int) (100 - progress) + "%");&nbsp; &nbsp; } else if (firstClicked == 0 && secondClicked == 0) {&nbsp; &nbsp; &nbsp; &nbsp; progress = 50;&nbsp; &nbsp; } else if (firstClicked == 0) {&nbsp; &nbsp; &nbsp; &nbsp; progress = 0;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; progress = 100;&nbsp; &nbsp; }&nbsp; &nbsp; percent1.setText((int) progress + "%");&nbsp; &nbsp; percent2.setText((int) (100 - progress) + "%");&nbsp; &nbsp; myBar.setProgress((int) progress, true);}}(不要忘记自动导入所有的东西)res/drawable文件夹中需要创建custom_progressbar.xml<layer-list xmlns:android="http://schemas.android.com/apk/res/android"><!-- Define the background properties like color etc --><item android:id="@android:id/background">&nbsp; &nbsp; <shape>&nbsp; &nbsp; &nbsp; &nbsp; <gradient&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:startColor="#000001"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:centerColor="#0b131e"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:centerY="1.0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:endColor="#0d1522"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:angle="270"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; </shape></item><!-- Define the progress properties like start color, end color etc --><item android:id="@android:id/progress">&nbsp; &nbsp; <clip>&nbsp; &nbsp; &nbsp; &nbsp; <shape>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <gradient&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:startColor="#007A00"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:centerColor="#007A00"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:centerY="1.0"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:endColor="#06101d"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; android:angle="270"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; </shape>&nbsp; &nbsp; </clip></item>res/layout文件夹中的activity_main.sml<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout&nbsp;&nbsp; &nbsp; xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context=".MainActivity"><ProgressBar&nbsp; &nbsp; android:id="@+id/progressBar"&nbsp; &nbsp; style="?android:attr/progressBarStyleHorizontal"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginStart="25dp"&nbsp; &nbsp; android:layout_marginLeft="25dp"&nbsp; &nbsp; android:layout_marginTop="25dp"&nbsp; &nbsp; android:layout_marginEnd="25dp"&nbsp; &nbsp; android:layout_marginRight="25dp"&nbsp; &nbsp; android:progress="50"&nbsp; &nbsp; android:progressDrawable="@drawable/custom_progressbar"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.0"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toTopOf="parent" /><Button&nbsp; &nbsp; android:id="@+id/btn1"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginTop="80dp"&nbsp; &nbsp; android:background="#007A00"&nbsp; &nbsp; android:text="@string/button_one"&nbsp; &nbsp; android:textColor="#FCF8F8"&nbsp; &nbsp; android:textStyle="bold"&nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@+id/btn2"&nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.5"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/progressBar" /><Button&nbsp; &nbsp; android:id="@+id/btn2"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginTop="80dp"&nbsp; &nbsp; android:background="#0B131E"&nbsp; &nbsp; android:text="@string/button_two"&nbsp; &nbsp; android:textColor="#FFFFFF"&nbsp; &nbsp; android:textStyle="bold"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.5"&nbsp; &nbsp; app:layout_constraintStart_toEndOf="@+id/btn1"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/progressBar" /><TextView&nbsp; &nbsp; android:id="@+id/score1"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginTop="15dp"&nbsp; &nbsp; android:text="@string/_0"&nbsp; &nbsp; android:textColor="#000000"&nbsp; &nbsp; app:layout_constraintEnd_toStartOf="@+id/score2"&nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.5"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/btn1" /><TextView&nbsp; &nbsp; android:id="@+id/score2"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginTop="15dp"&nbsp; &nbsp; android:text="0"&nbsp; &nbsp; android:textColor="#000000"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintHorizontal_bias="0.5"&nbsp; &nbsp; app:layout_constraintStart_toEndOf="@+id/score1"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/btn2" /><TextView&nbsp; &nbsp; android:id="@+id/percent1"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginStart="25dp"&nbsp; &nbsp; android:layout_marginLeft="25dp"&nbsp; &nbsp; android:layout_marginTop="10dp"&nbsp; &nbsp; android:text="50%"&nbsp; &nbsp; android:textColor="#007A00"&nbsp; &nbsp; android:textSize="22sp"&nbsp; &nbsp; android:textStyle="bold"&nbsp; &nbsp; app:layout_constraintStart_toStartOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/progressBar" /><TextView&nbsp; &nbsp; android:id="@+id/percent2"&nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; android:layout_marginTop="10dp"&nbsp; &nbsp; android:layout_marginEnd="25dp"&nbsp; &nbsp; android:layout_marginRight="25dp"&nbsp; &nbsp; android:text="50%"&nbsp; &nbsp; android:textColor="#0B131E"&nbsp; &nbsp; android:textSize="22sp"&nbsp; &nbsp; android:textStyle="bold"&nbsp; &nbsp; app:layout_constraintEnd_toEndOf="parent"&nbsp; &nbsp; app:layout_constraintTop_toBottomOf="@+id/progressBar" /></android.support.constraint.ConstraintLayout>请记住,我的项目称为ProgressBar,因此如果您将空项目称为ProgressBar ,从技术上讲,您不需要替换任何其他内容。您可以从代码中的所有细节中学习,但这基本上是您要求完成的任务。如果您难以理解某些事情,请使用Ctrl+Shift+FWindows 和Command+Shift+FMac 跨项目搜索内容。
随时随地看视频慕课网APP

相关分类

Java
我要回答