在 Android 的 MainActivity.java 文件中哪里添加功能?
我目前正在学习 Android 开发,并且对在 MainActivity.java 文件中添加某些功能的位置感到困惑。
一些背景: 我正在使用 Java 在 Android 中设计一个基本的咖啡应用程序。我在 XML 文件中添加了一个配料复选框。
<CheckBox
android:id="@+id/checkbox_whipped_cream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Whipped Cream"
android:paddingLeft="20dp"
android:textSize="25dp" />
当我单击应用程序上的订单按钮时,我应该能够看到此生奶油复选框已勾选并显示在订单摘要中。
那么,通常应该在 .java 文件中的哪里添加功能呢?
这是我到目前为止编写的 java 代码:
package android.example.updated_coffee_app;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
int quantity = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void increment(View view) {
quantity = quantity + 1;
display(quantity);
}
public void decrement(View view) {
quantity = quantity - 1;
display(quantity);
}
public void order(View view) {
CheckBox whippedCream = (CheckBox) findViewById(R.id.checkbox_whipped_cream);
int price = calculatePrice();
String priceMessage = createOrderSummary(price);
displayMessage(createOrderSummary(price));
}
private String createOrderSummary(int price) {
String priceMessage = "Name : Toshali ";
priceMessage = priceMessage + "\nCoffee Quantity: " + quantity;
priceMessage = priceMessage + "\nTotal: $" + price;
priceMessage = priceMessage + "\nThank You!";
return priceMessage;
}
private int calculatePrice() {
int price = 5 * quantity;
return price;
}
public void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);
}
private void display(int numbers) {
TextView quantityTextView = (TextView) findViewById(R.id.quantityOfCoffee);
quantityTextView.setText(" " + numbers);
}
}
一般来说,我很困惑在哪里添加一段特定的代码?用什么方法(函数)?或者我们是否为所有组件制定单独的方法?
在图像中,我应该能够看到“生奶油?” :订单摘要中的“正确”。
I am currently learning Android Development and am confused about where to add certain functionality in the MainActivity.java file.
Some context:
I am designing a basic coffee app in Android using Java. I have added a checkbox for toppings in the XML file.
<CheckBox
android:id="@+id/checkbox_whipped_cream"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Whipped Cream"
android:paddingLeft="20dp"
android:textSize="25dp" />
When I click the order button on my app, I should be able to see this Whipped Cream checkbox ticked and displayed in the order summary.
So, where should in general one add the functionality in the .java file?
Here's the java code that I've written so far:
package android.example.updated_coffee_app;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
int quantity = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void increment(View view) {
quantity = quantity + 1;
display(quantity);
}
public void decrement(View view) {
quantity = quantity - 1;
display(quantity);
}
public void order(View view) {
CheckBox whippedCream = (CheckBox) findViewById(R.id.checkbox_whipped_cream);
int price = calculatePrice();
String priceMessage = createOrderSummary(price);
displayMessage(createOrderSummary(price));
}
private String createOrderSummary(int price) {
String priceMessage = "Name : Toshali ";
priceMessage = priceMessage + "\nCoffee Quantity: " + quantity;
priceMessage = priceMessage + "\nTotal: quot; + price;
priceMessage = priceMessage + "\nThank You!";
return priceMessage;
}
private int calculatePrice() {
int price = 5 * quantity;
return price;
}
public void displayMessage(String message) {
TextView orderSummaryTextView = (TextView) findViewById(R.id.order_summary_text_view);
orderSummaryTextView.setText(message);
}
private void display(int numbers) {
TextView quantityTextView = (TextView) findViewById(R.id.quantityOfCoffee);
quantityTextView.setText(" " + numbers);
}
}
In general, I'm confused about where does one add a particular piece of code? In what method(function)? Or do we make separate methods for all the components?
In the image, I should be able to see 'Whipped Cream? : True' in the order summary.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,您仅在
订单
运行时检查复选框的状态。如果您不想对选中/取消选中的复选框做出反应,则需要指定一个侦听器。
复选框允许您在 XML 中指定一个方法,每当其状态随“
android:onClick=
”发生变化时就会调用该方法。您可以这样更改代码:
在 xml 布局中,将其添加到
CheckBox
元素中:在您的 Activity 中,添加此新方法:
参考: https://developer.android.com/guide/topics/ui/controls/checkbox#java
Android 上的意见应用程序代码结构
你应该有单独的对 UI 事件做出反应的方法,例如单击按钮和切换复选框。理想情况下,这些方法应该更新 UI 状态对象。
Google 固执己见的建议是,UI 的状态应由 UI 状态数据类确定。在您的示例中,UI 状态类将保存价格、数量和所选附加项目(生奶油)的数据。
然后,按下按钮等事件将更新 UI 状态对象。每当您的 UI 状态发生更改时,您都会更新 UI。
请在此处查看本指南:https://developer.android。 com/jetpack/guide/ui-layer#define-ui-state
Right now you only check the state of your checkbox when the
order
runs.If you want to isntantly react to the checkbox being checked / unchecked, you need to specify a listener.
Checkboxes allow you to specify a method in the XML, which is called whenever its state changes with "
android:onClick=
".You can change your code like this:
In your xml layout, add this to your
CheckBox
element:In your activity, add this new method:
Reference: https://developer.android.com/guide/topics/ui/controls/checkbox#java
Opinion on Android app code structure
You should have separate methods which react to UI events like clicking buttons and toggeling checkboxes. Ideally, those methods should update a UI state object.
Google's opinionated recommendation is that the state of your UI should be determined by UI state data classes. In your example a UI state class would hold the data for the price, the quantity and the selected extras (whipped cream).
Events like button presses will then update the UI state object. Whenever your UI state changes, you update the UI.
Check out this guide here: https://developer.android.com/jetpack/guide/ui-layer#define-ui-state