我将如何在按钮元素的文本字段(或 TextView)内显示 Long 或 Int?在文本字段中显示字符串是否可以使用相同的方法?谢谢。
这是我的 MainActivity 代码
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.StatFs;
import android.view.MenuInflater;
import android.view.View;
import android.view.Menu;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.material.navigation.NavigationView;
import androidx.appcompat.widget.PopupMenu;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import java.io.File;
public class MainActivity extends AppCompatActivity {
Button mainstorage;
private AppBarConfiguration mAppBarConfiguration;
private static final int REQUEST_WRITE_STORAGE = 112;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
StatFs statFs = new
StatFs(Environment.getRootDirectory().getAbsolutePath());
long blockSize = statFs.getBlockSize();
long totalSize = statFs.getBlockCount()*blockSize;
long availableSize = statFs.getAvailableBlocks()*blockSize;
long freeSize = statFs.getFreeBlocks()*blockSize;
String internalSize = Long.toString(blockSize);
myBtn.setText(internalSize);
}
}
还有我的 fragment XML。
<androidx.constraintlayout.widget.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=".ui.home.HomeFragment">
<LinearLayout
android:background="@android:color/transparent"
android:id="@+id/firstrow"
android:layout_height="0dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="start"
app:layout_constraintHeight_percent="0.22"
tools:ignore="MissingConstraints">
<Button
android:contentDescription="@string/goInternalDesc"
android:id="@+id/main"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@android:color/transparent"
android:drawableTop="@drawable/storage"
android:minWidth="20dp"
android:text="Main Storage"
android:minHeight="20dp" />
</LinearLayout>
我想知道为什么在我的代码中尝试将按钮的文本设置为字符串时出现构建错误。我认为这可能是因为我在 XML 中设置了按钮的文本,但删除它没有效果——我的 MainActivity 上仍然出现“无法解析符号”错误。
最佳答案
您可以使用
btn.setText(String.valueOf(myLong));
btn.setText(String.valueOf(myInt));
或者
btn.setText(Long.toString(myLong));
btn.setText(Integer.toString(myInt));
关于java - Android Studio 在按钮文本中显示长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62454253/