第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Android 百分比欄 onclicklistener

Android 百分比欄 onclicklistener

叮當(dāng)貓咪 2023-06-08 14:49:47
我是 Java 和編碼方面的新手,我嘗試學(xué)習(xí)如何在 Android Studio 中編碼并嘗試一些練習(xí)。我想為 2 個按鈕創(chuàng)建代碼:bt1 和 bt2。當(dāng)用戶點擊 bt1 時,進度條顯示點擊此 bt1 的百分比。此解決方案的任何其他幫助/方法也將是可觀的。
查看完整描述

1 回答

?
慕斯709654

TA貢獻1840條經(jīng)驗 獲得超5個贊

因此,據(jù)我從你的描述和我們的談話中了解到,你需要一個進度條來顯示點擊第一個和第二個按鈕之間的百分比。如果您創(chuàng)建一個 Empty Activity 項目并替換以下文件中的所有這些內(nèi)容:


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

}

}


(不要忘記自動導(dǎo)入所有的東西)


res/drawable文件夾中需要創(chuàng)建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 ,從技術(shù)上講,您不需要替換任何其他內(nèi)容。


您可以從代碼中的所有細(xì)節(jié)中學(xué)習(xí),但這基本上是您要求完成的任務(wù)。如果您難以理解某些事情,請使用Ctrl+Shift+FWindows 和Command+Shift+FMac 跨項目搜索內(nèi)容。


查看完整回答
反對 回復(fù) 2023-06-08
  • 1 回答
  • 0 關(guān)注
  • 163 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號