gpt4 book ai didi

java - 在一个公共(public)方法中使用相同的私有(private)方法

转载 作者:行者123 更新时间:2023-12-02 01:59:29 24 4
gpt4 key购买 nike

请看下面的代码。我正在创建一个记分应用程序,并将其设置为记录每个团队的某些统计数据。因此,当我点击一支球队的+按钮时,我希望它更新该统计数据,然后为另一支球队更新相同的内容,而不更新第一支球队。我还附上了应用程序本身的屏幕截图(请原谅现在的着色,哈哈)。请问有人可以提供帮助吗? Football Score Counter App

所以基本上:如果我选择主队下的任意球选项,我希望它更新该选项,如果客队获得点球,我希望能够更新该选项,而无需同时更新两支球队时间。或者只更新一个团队。因为这就是它现在正在做的事情。

Android Studio 告诉我我不能有“private void display(int number)”,因为它与下面的另一个匹配。那么如何区分它们呢?

非常感谢,我希望我已尽力正确解释!

public void increment(View view) {
quantity = quantity + 1;
display(quantity);
}

/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.quantity_text_view);
quantityTextView.setText("" + number);
}

/**
* This method displays the given quantity value on the screen.
*/
private void display(int number) {
TextView quantityTextView = (TextView) findViewById(R.id.amount_text_view);
quantityTextView.setText("" + number);
}

我对此进行了编辑,还添加了正在使用的部分 XML 代码:

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_weight="1"
android:background="#4CAF50"
android:orientation="horizontal">

<Button
android:id="@+id/decrement_id"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="10dp"

android:layout_weight="1"
android:onClick="decrement"
android:text="-" />

<TextView
android:id="@+id/quantity_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="0"
android:textColor="#000000"
android:textSize="20sp" />

<Button
android:id="@+id/increment_id"
android:layout_width="48dp"
android:layout_height="48dp"

android:layout_weight="1"
android:onClick="increment"
android:text="+" />

<TextView
android:id="@+id/freeKick_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="FREE KICK"
android:textColor="#000000"
android:textSize="16sp" />

<Button
android:id="@+id/decrease_id"
android:layout_width="48dp"
android:layout_height="48dp"

android:layout_weight="1"
android:onClick="decrement"
android:text="-" />

<TextView
android:id="@+id/amount_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"

android:text="0"
android:textColor="#000000"
android:textSize="20sp" />

<Button
android:id="@+id/increase_id"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:onClick="increment"
android:text="+" />

</LinearLayout>

所以在这里,我想说的是,两支球队的任意球都需要更新,但前提是每支球队都获得任意球。所以当我按下主队的+按钮时,他们就获得了任意球。然后,如果客队获得任意球,则为他们点击+按钮。希望这能让事情变得更清楚! :)

最佳答案

避免在两个方法中重复类似功能的一种方法是向方法签名添加另一个参数:

/**
* This method displays the given quantity value on the screen.
*/
private void display(int number, @IntegerRes int viewId) {
TextView quantityTextView = (TextView) findViewById(viewId);
quantityTextView.setText("" + number);
}

然后,您可以使用以下方法:

public void increment(View view) {
quantity = quantity + 1;
display(quantity, R.id.quantity_text_view);
display(quantity, R.id.amount_text_view);
}

关于java - 在一个公共(public)方法中使用相同的私有(private)方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51849393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com