gpt4 book ai didi

java - 将代码从 Java 转换为 Android Studio(显示值)

转载 作者:太空宇宙 更新时间:2023-11-04 12:10:14 24 4
gpt4 key购买 nike

我正在尝试将我的游戏从 java 转换为 Android Studio,这样我就可以学习 Android 开发。但我现在最大的问题是显示值。

我有变量,例如:CarMain.main[0]

使用按钮添加值:CarMain.main[0] += 1;

在 Java 中,我显示的值如下:

S_MoneyLabel.setText("Money: " + CarMain.main[0]); 

输出例如:Money : 71 或 Money :5000

现在在Android studio中我不知道如何显示它。我正在考虑 textview 选项,但不知道该怎么做。

最佳答案

在布局 xml 文件中添加 TextView ,如下所示:

    <RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button"/>
</RelativeLayout>

现在,在您的 Activity 类中,在 onCreate() 方法中为该按钮创建实例:

    Button button = findViewById(R.id.button);

现在,将结果设置为 TextView

    button.setText("Money: " + CarMain.main[0]);

关于java - 将代码从 Java 转换为 Android Studio(显示值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39933094/

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