简体   繁体   English

在哪里为 android 添加 MainActivity.java 文件中的功能?

[英]Where does one add a functionality in the MainActivity.java file for android?

I am currently learning Android Development and am confused about where to add certain functionality in the MainActivity.java file.我目前正在学习 Android 开发,并且对在 MainActivity.java 文件中的何处添加某些功能感到困惑。

Some context: I am designing a basic coffee app in Android using Java. I have added a checkbox for toppings in the XML file.一些上下文:我正在 Android 中使用 Java 设计一个基本的咖啡应用程序。我在 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" />

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?那么,通常应该在哪里添加 .java 文件中的功能?

Here's the java code that I've written so far:这是我到目前为止编写的 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);
    }

}

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. : True' 在订单摘要中。

Whipped Cream Checkbox option鲜奶油复选框选项

Right now you only check the state of your checkbox when the order runs.现在您只在order运行时选中复选框的 state。 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= ".复选框允许您在 XML 中指定一个方法,只要其 state 更改为“ android:onClick= ”,就会调用该方法。

You can change your code like this:您可以像这样更改代码:

In your xml layout, add this to your CheckBox element:在您的 xml 布局中,将其添加到您的CheckBox元素:

android:onClick="onCheckboxClicked"

In your activity, add this new method:在您的活动中,添加这个新方法:

public void onCheckboxClicked(View view) {
    boolean checked = ((CheckBox) view).isChecked();
    if(checked) {
        // TODO: Show that whipped cream is selected
    }
    else {
        // TODO: Show that whipped cream is not selected
    }
}

Reference: https://developer.android.com/guide/topics/ui/controls/checkbox#java参考: https://developer.android.com/guide/topics/ui/controls/checkbox#java

Opinion on Android app code structure对Android app代码结构的看法

You should have separate methods which react to UI events like clicking buttons and toggeling checkboxes.您应该有单独的方法来响应 UI 事件,例如单击按钮和切换复选框。 Ideally, those methods should update a UI state object.理想情况下,这些方法应该更新 UI state object。

Google's opinionated recommendation is that the state of your UI should be determined by UI state data classes. Google 固执己见的建议是,您的 UI 的 state 应该由 UI state 数据类确定。 In your example a UI state class would hold the data for the price, the quantity and the selected extras (whipped cream).在您的示例中,UI state class 将保存价格、数量和所选附加物(鲜奶油)的数据。

Events like button presses will then update the UI state object. Whenever your UI state changes, you update the UI.按下按钮等事件将更新 UI state object。每当您的 UI state 更改时,您都会更新 UI。

Check out this guide here: https://developer.android.com/jetpack/guide/ui-layer#define-ui-state在此处查看本指南: https://developer.android.com/jetpack/guide/ui-layer#define-ui-state

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM