为了账号安全,请及时绑定邮箱和手机立即绑定

Android 百分比栏 onclicklistener

Android 百分比栏 onclicklistener

叮当猫咪 2023-06-08 14:49:47
我是 Java 和编码方面的新手,我尝试学习如何在 Android Studio 中编码并尝试一些练习。我想为 2 个按钮创建代码:bt1 和 bt2。当用户点击 bt1 时,进度条显示点击此 bt1 的百分比。此解决方案的任何其他帮助/方法也将是可观的。
查看完整描述

1 回答

?
慕斯709654

TA贡献1840条经验 获得超5个赞

因此,据我从你的描述和我们的谈话中了解到,你需要一个进度条来显示点击第一个和第二个按钮之间的百分比。如果您创建一个 Empty Activity 项目并替换以下文件中的所有这些内容:


MainActivity.java


public 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)

@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);


    btn1 = findViewById(R.id.btn1);

    btn2 = findViewById(R.id.btn2);


    myBar = findViewById(R.id.progressBar);


    score1 = findViewById(R.id.score1);

    score2 = findViewById(R.id.score2);


    percent1 = findViewById(R.id.percent1);

    percent2 = findViewById(R.id.percent2);



    firstClicked = 0;

    secondClicked = 0;


    btn1.setOnClickListener(new View.OnClickListener() {

        @RequiresApi(api = Build.VERSION_CODES.N)

        @Override

        public void onClick(View v) {

            firstClicked++;

            score1.setText(String.valueOf((int) firstClicked));

            updateProgressTab();

        }

    });


    btn2.setOnClickListener(new View.OnClickListener() {

        @RequiresApi(api = Build.VERSION_CODES.N)

        @Override

        public void onClick(View v) {

            secondClicked++;

            score2.setText(String.valueOf((int) secondClicked));

            updateProgressTab();

        }

    });


    updateProgressTab();

}


@SuppressLint("SetTextI18n")

@RequiresApi(api = Build.VERSION_CODES.N)

public void updateProgressTab() {

    float progress;


    if (firstClicked != 0 && secondClicked != 0) {

        progress = (firstClicked / (firstClicked + secondClicked)) * 100;

        percent1.setText((int) progress + "%");

        percent2.setText((int) (100 - progress) + "%");

    } else if (firstClicked == 0 && secondClicked == 0) {

        progress = 50;

    } else if (firstClicked == 0) {

        progress = 0;

    } else {

        progress = 100;

    }


    percent1.setText((int) progress + "%");

    percent2.setText((int) (100 - progress) + "%");


    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">

    <shape>

        <gradient

            android:startColor="#000001"

            android:centerColor="#0b131e"

            android:centerY="1.0"

            android:endColor="#0d1522"

            android:angle="270"

            />

    </shape>

</item>


<!-- Define the progress properties like start color, end color etc -->

<item android:id="@android:id/progress">

    <clip>

        <shape>

            <gradient

                android:startColor="#007A00"

                android:centerColor="#007A00"

                android:centerY="1.0"

                android:endColor="#06101d"

                android:angle="270"

                />

        </shape>

    </clip>

</item>

res/layout文件夹中的activity_main.sml


<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.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"

    tools:context=".MainActivity">


<ProgressBar

    android:id="@+id/progressBar"

    style="?android:attr/progressBarStyleHorizontal"

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:layout_marginStart="25dp"

    android:layout_marginLeft="25dp"

    android:layout_marginTop="25dp"

    android:layout_marginEnd="25dp"

    android:layout_marginRight="25dp"

    android:progress="50"

    android:progressDrawable="@drawable/custom_progressbar"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintHorizontal_bias="0.0"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toTopOf="parent" />


<Button

    android:id="@+id/btn1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginTop="80dp"

    android:background="#007A00"

    android:text="@string/button_one"

    android:textColor="#FCF8F8"

    android:textStyle="bold"

    app:layout_constraintEnd_toStartOf="@+id/btn2"

    app:layout_constraintHorizontal_bias="0.5"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toBottomOf="@+id/progressBar" />


<Button

    android:id="@+id/btn2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginTop="80dp"

    android:background="#0B131E"

    android:text="@string/button_two"

    android:textColor="#FFFFFF"

    android:textStyle="bold"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintHorizontal_bias="0.5"

    app:layout_constraintStart_toEndOf="@+id/btn1"

    app:layout_constraintTop_toBottomOf="@+id/progressBar" />


<TextView

    android:id="@+id/score1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginTop="15dp"

    android:text="@string/_0"

    android:textColor="#000000"

    app:layout_constraintEnd_toStartOf="@+id/score2"

    app:layout_constraintHorizontal_bias="0.5"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toBottomOf="@+id/btn1" />


<TextView

    android:id="@+id/score2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginTop="15dp"

    android:text="0"

    android:textColor="#000000"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintHorizontal_bias="0.5"

    app:layout_constraintStart_toEndOf="@+id/score1"

    app:layout_constraintTop_toBottomOf="@+id/btn2" />


<TextView

    android:id="@+id/percent1"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginStart="25dp"

    android:layout_marginLeft="25dp"

    android:layout_marginTop="10dp"

    android:text="50%"

    android:textColor="#007A00"

    android:textSize="22sp"

    android:textStyle="bold"

    app:layout_constraintStart_toStartOf="parent"

    app:layout_constraintTop_toBottomOf="@+id/progressBar" />


<TextView

    android:id="@+id/percent2"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginTop="10dp"

    android:layout_marginEnd="25dp"

    android:layout_marginRight="25dp"

    android:text="50%"

    android:textColor="#0B131E"

    android:textSize="22sp"

    android:textStyle="bold"

    app:layout_constraintEnd_toEndOf="parent"

    app:layout_constraintTop_toBottomOf="@+id/progressBar" />

</android.support.constraint.ConstraintLayout>

请记住,我的项目称为ProgressBar,因此如果您将空项目称为ProgressBar ,从技术上讲,您不需要替换任何其他内容。


您可以从代码中的所有细节中学习,但这基本上是您要求完成的任务。如果您难以理解某些事情,请使用Ctrl+Shift+FWindows 和Command+Shift+FMac 跨项目搜索内容。


查看完整回答
反对 回复 2023-06-08
  • 1 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信